Game Development Reference
In-Depth Information
In Listing 8-18, we see the task
doSetup
for the class
PanGestureController
. The first thing we
do after specifying the size of the game area is specify the
minYValue
and the
maxYValue
for the
asteroids, so they cannot move within 72 points of the top or bottom of the screen. Next, we create
our three Asteroid objects, add them to the scene, and add them to the
NSMutableArray asteroids
.
Each Asteroid starts at the top of the screen.
The last thing we do in
doSetup
is create a
UIPanGestureRecognizer
, configure it to call
panGesture
:,
and add it to
actorsView
. The next section explains how we respond to these gestures to create the
desired behavior.
Responding to Pan Gestures
We have reviewed how to set up the class
UIPanGestureController
. Now we will explore how to
respond to this gesture in the task
panGesture
:, as shown in Listing 8-19.
Listing 8-19. PanGestureController.m (panGesture:)
-(void)panGesture:(UIPanGestureRecognizer*)panRecognizer{
if ([panRecognizer state] == UIGestureRecognizerStateBegan){
CGSize gameAreaSize=[self gameAreaSize];
CGPoint locationInView = [panRecognizer locationInView:actorsView];
if (locationInView.x<gameAreaSize.width/3.0){
asteroidIndex=0;
} else if (locationInView.x>gameAreaSize.width/3.0*2){
asteroidIndex=2;
} else {
asteroidIndex=1;
}
Asteroid* asteroid=[asteroids objectAtIndex:asteroidIndex];
startCenter=[asteroid center];
[asteroid setAnimationPaused:YES];
} else if ([panRecognizer state] == UIGestureRecognizerStateChanged){
Asteroid* asteroid=[asteroids objectAtIndex:asteroidIndex];
CGPoint locationInView = [panRecognizer locationInView:actorsView];
CGPoint center=[asteroid center];
center.y=locationInView.y;
if (center.y<[self minYValue]){
center.y=[self minYValue];
}
if (center.y>[self maxYValue]){
center.y=[self maxYValue];
}
[asteroid setCenter:center];
} else if ([panRecognizer state] == UIGestureRecognizerStateEnded){