Game Development Reference
In-Depth Information
if (point.y < self.center.y){
[self setState:VPR_STATE_UP];
} else if (point.y > self.center.y){
[self setState:VPR_STATE_DOWN];
}
motion = [LinearMotion linearMotionFromPoint:[self center] toPoint:point AtSpeed:1.2f];
[motion setDelegate:self];
[motion setStopAtPoint:YES];
[motion setPointToStopAt:point];
[motion setStayInRect:YES];
CGSize gameSize = [gameController gameAreaSize];
[motion setRectToStayIn:CGRectMake(63, 31, 2, gameSize.height - 63)];
[motion setWrap:NO];
[[self behaviors] addObject:motion];
setMoveToPoint:within:
. In this task, the goal is to set up a
object that will move the
Viper
to the specified point. The first step is to see if we have
LinearMotion
object stored at the variable motion. If so, we want to remove it from our
CGPoint
called point, based on the
CGPoint
find a point that keeps the
Viper
horizontally fixed. Once we know where our point is, we can set our
state to either
VPR_STATE_UP
or
VPR_STATE_DOWN
.
Creating the new
LinearMotion
is as simple as passing the
Viper's
current
center
and
point
to the
constructor
linearMotionFromPoint:toPoint:AtSpeed:
. Setting self as the
delegate
, and telling it to
stop when it reaches the specified point, further modifies the
LinearMotion
. The last modification is
to set
LinearMotion
so that it stays within the specified rectangle. This prevents the
Viper
from going
outside the bounds of the screen.
We have looked at how the
Viper
moves about the screen. The rest of the interesting features are
implemented in the task
step:
, as shown in Listing 12-20.
Listing 12-20. Viper.m (step:)
-(void)step:(GameController*)gameController{
long stepNumber = [gameController stepNumber];
if (stepNumber - lastShot > stepsPerShot - damage){
[gameController playAudio:AUDIO_BLIP];
CGPoint center = [self center];
CGSize gameSize = [gameController gameAreaSize];