Game Development Reference
In-Depth Information
Asteroid* asteroid=[asteroids objectAtIndex:asteroidIndex];
[asteroid setAnimationPaused:NO];
} else if ([panRecognizer state] == UIGestureRecognizerStateCancelled){
Asteroid* asteroid=[asteroids objectAtIndex:asteroidIndex];
[asteroid setAnimationPaused:NO];
[asteroid setCenter:startCenter];
}
}
In Listing 8-19, we can see that the task
panGesture
: is broken up by the state of
panRecognizer
in the same way as for the pinch-out example. If the state of
panRecognizer
equals
UIGestureRecognizerStateBegan
, we calculate which of the three asteroids the gesture is closest
asteroidIndex
, we can get
NSMutableArray
asteroids and record its location in
startCenter
.
animationPaused
to No.
panGesture
, the
panRecognizer
will have the state of
. When this happens, we simply set the Y value of the selected
locationInView of panRecognizer
.
or
UIGestureRecognizerStateCancelled
. In both cases, we want to
.
I hope by this point a pattern is emerging as to how these gesture recognizers work. The basic
pattern is to set up the recognizer to detect gestures on a
UIView
, configure the callback task to
record the starting state of whatever is being manipulated (saucer, asteroid, and so on), then apply
changes to the scene or roll back the changes in the case of cancellation. Let's look at the rotation
gesture next.
Rotation Gesture
The rotation gesture is not as common as the previously covered gestures in the core
iOS
applications. In fact, I could not find a single example in the applications that ship with the
iPhone
,
but I may be overlooking something. The rotation gesture, however, does show up in a number
puzzle games, where you need to rotate an object this way or that. The rotation gesture is performed
by putting two fingers on the screen and spinning your hand, like you are opening a bottle. Figure
8-5
shows a rotation example.
In Figure
8-5
we see a spaceship being rotated counterclockwise. We know the ship is being rotated
counterclockwise because its starboard-maneuvering thruster is firing. If the ship were rotating
clockwise, we would see the port-maneuvering thruster firing. The class that is responsible for this
example is
RotationGestureController
, whose header file is shown in Listing 8-20.