Game Development Reference
In-Depth Information
Listing 8-30. ShakeController.m (doSetup)
-(BOOL)doSetup{
if ([super doSetup]){
[self setGameAreaSize:CGSizeMake(320, 480)];
NSMutableArray* classes=[NSMutableArray new];
[classes addObject:[Asteroid class]];
[self setSortedActorClasses:classes];
return YES;
}
return NO;
}
In Listing 8-30, we see the task doSetup that configures the ShakeController , marking Asteroid as a
class of actor we will want to get access to later. We add our Asteroid in the task updateScene . We
do this since we want to add a new Asteroid only when there are no Asteroid objects in the scene,
as can be seen in Listing 8-31.
Listing 8-31. ShakeController (updateScene)
-(void)updateScene{
if ([[self actorsOfType:[Asteroid class]] count] ==0 ){
Asteroid* asteroid = [Asteroid asteroid:self];
[self addActor:asteroid];
}
[super updateScene];
}
In Listing 8-31, we see the updateScene task that is called in order to move the game forward
through each step of the animation. In this task, we check to see if there are Asteroid objects in
the scene; if there are none, we go ahead and add one. Lastly, we call the super-implementation of
updateScene to drive the game forward.
Now the question is, How do we respond to shake events so we can blow up the asteroid? By
informing the application that the top view can become the “first responder” and implement
the appropriate method to respond to a motion event, we achieve this. A UIView that is the first
responder is a UIView that gets the first crack at interpreting user input. There are two steps to
getting a view to be first responder: first, you have to inform the application that the view property
of a UIViewController can be the first responder; then, you have to make a request to become first
responder. Listing 8-32 shows the code to accomplish this.
Listing 8-32. ShakeController (first responder related tasks)
-(BOOL)canBecomeFirstResponder {
return YES;
}
 
Search WWH ::




Custom Search