Game Development Reference
In-Depth Information
each is implemented with the same base class,
Particle
. Let's take a closer look at the class
Example03Controller
to better understand how this example works before diving into the details of
each new actor. Listing 7-14, shows the important parts of
Example03Controller
.
Listing 7-14. Example03Controller.m (doSetup and updateScene)
-(BOOL)doSetup{
if([super doSetup]){
NSMutableArray* classes = [NSMutableArray new];
[classes addObject:[Asteroid class]];
[self setSortedActorClasses:classes];
UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(tapGesture:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setNumberOfTouchesRequired:1];
[actorsView addGestureRecognizer:tapRecognizer];
return YES;
}
return NO;
}
-(void)updateScene{
if (self.stepNumber % (60*5) == 0){
[self addActor:[Comet comet:self]];
}
if ([[self actorsOfType:[Asteroid class]] count] == 0){
int count = arc4random()%4+1;
for (int i=0;i<count;i++){
[self addActor:[Asteroid asteroid:self]];
}
}
[super updateScene];
}
In Listing 7-14, we see two tasks of the class
Example03Controller
:
doSetup
and
updateScene
.
The task
doSetup
follows our now familiar pattern of identifying the actor classes we want efficient
access to—in this case,
Asteroid
, by calling the task
setSortedActorClass:
. We also create a
UITapGestureRecognizer
called
tapRecognizer
and register that with the
actorsView
object. In this
way, the task
tapGesture:
will be called when we tap the screen.
The task
updateScene
, as shown in Listing 7-14, adds a new
Comet
actor every five seconds (60*5)
and adds one to four Asteroids whenever there are zero
Asteroids
in the scene. As with the previous
examples, the code in the controller class is relatively simple. Most of the real logic for the example
lives in the different actor classes. Before we look at the
Asteroid
class, let's consider the task
tapGesture:
, shown in Listing 7-15.