Game Development Reference
In-Depth Information
Listing 12-13. BeltCommanderController.m (doAddNewTrouble)
-(void)doAddNewTrouble{
if ([gameParameters includeAsteroids] && arc4random() % (5*60) == 0){
if ([[self actorsOfType:[Asteroid class]] count] < 20){
[self addActor:[Asteroid asteroid:self]];
}
}
if ([gameParameters includeSaucers] && arc4random() % (10*60) == 0){
if ([[self actorsOfType:[Saucer class]] count] < 3){
[self addActor:[Saucer saucer:self]];
}
}
if ([gameParameters includePowerups] && arc4random() % (20*60) == 0){
[self addActor:[Powerup powerup:self]];
}
doAddNewTrouble
that is responsible for adding new
Actors
to
Actors
we might add, we first check to see if
gameParameters
gameParameters
indicates that we should add an
Actor
of a
Asteroids
Actor
we create it and call
addActor:
. The
Actor's
Actor
is created. We will now move on and
Collision Detection
Now we know how each Actor is added to the scene. Let's move on and look at how they interact.
If we consider the five main Actors in this game, there are several interactions that must be
considered, as follows:
Bullets
and
Asteroids
Asteroids
and the
Viper
Bullets
and the
Viper
Bullets
and
Saucers
Powerups
and the
Viper
These interactions are handled in the task
doCollisionDetection
, the first part of which is shown in
Listing 12-14.
Listing 12-14. BeltCommanderController.m (doCollisionDetection, part 1)
-(void)doCollisionDetection{
NSSet* bullets = [self actorsOfType:[Bullet class]];
NSSet* asteroids = [self actorsOfType:[Asteroid class]];
NSSet* saucers = [self actorsOfType:[Saucer class]];
NSSet* powerups = [self actorsOfType:[Powerup class]];