Game Development Reference
In-Depth Information
In Listing 8-23, we see the header for the class
LongPressController
, which shows that we have
a reference to the spaceship, as well as an
NSDate
that is used to keep track of when a long press
gesture starts. The task
tapGesture
: is used when the user simply taps the screen to fire a small
bullet. The task
longPressGesture
: is called when the user performs a long press gesture to fire
a larger bullet. The last task is used to create a Bullet actor and add it to the scene. Let's start by
looking at the
doSetup
task of
LongPressController
. See in Listing 8-24.
Listing 8-24. LongPressController.m (doSetup)
-(BOOL)doSetup{
if ([super doSetup]){
[self setGameAreaSize:CGSizeMake(320, 480)];
viper=[Viper viper:self];
[self addActor:viper];
CGPoint center=[viper center];
center.y=[self gameAreaSize].height/5.0*4.0;
[viper setCenter:center];
UITapGestureRecognizer* tapRecognizer=[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(tapGesture:)];
[actorsView addGestureRecognizer:tapRecognizer];
UILongPressGestureRecognizer* longPressRecognizer=[[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(longPressGesture:)];
[longPressRecognizer setMinimumPressDuration:1.0f];
[actorsView addGestureRecognizer: longPressRecognizer];
return YES;
}
return NO;
}
In Listing 8-24, we see task
doSetup
. After we perform the usual steps of setting the game
area size and adding the actor Viper, we register two gesture responders. The first gesture
recognizer is a
UITapGestureRecognizer
that will call
tapGesture
:. The second gesture
recognizer is a
UILongPressGestureRecognizer
that is configure to call
longPressGesture
:. The
UILongPressGestureRecognizer
has the property
minimumPressDuration
set to 1.0 f. This means that
the user must hold the press for one second before this
UILongPressGestureRecognizer
will consider
the touch a long press. Both gesture recognizers are added to
actorsView
.
Responding to the User
In the following section, we will look at how bullets are added, based on which gesture was
triggered. Let's get the tap gesture out of the way first. The implementation of
tapGesture
: is shown
in Listing 8-25.