Game Development Reference
In-Depth Information
In Listing 7-19, we see the expected tasks for defining an image-based actor. We see that image
files will start with the string "Asteroid" as shown in the task baseImageName . We also see that there
are 31 images for each of the three variants, as shown in getFramesCountForVariant:AndState: and
getNameForVariant: .
The task, instance in Listing 7-19, is something new for us. This task represents a way for us to
create a singleton of the class AsteroidRepresentationDelegate . We do this by synchronizing on the
class object for Asteroid and only creating a new AsteroidRepresentationDelegate if the variable
instance is nil . This allows us to only create one instance of this class, even though we could have
hundreds of Asteroids and Particles using it to specify how they look. Let's now put all the pieces
together and look at how an Asteroid is destroyed.
Destroying an Asteroid
We have set the groundwork for understanding how an Asteroid is created and represented on the
screen. Let's take a look at the task doHit: to understand how these pieces are brought together to
create the desired behavior. See Listing 7-20.
Listing 7-20. Asteroid.m (doHit:)
-(void)doHit:(GameController*)controller{
if (level > 1){
int count = arc4random()%3+1;
for (int i=0;i<count;i++){
Asteroid* newAst = [Asteroid asteroidOfLevel:level-1 At:self.center];
[controller addActor:newAst];
}
}
int particles = arc4random()%5+1;
for (int i=0;i<particles;i++){
ImageRepresentation* rep = [ImageRepresentation imageRepWithDelegate:[AsteroidRepresentation
Delegate instance]];
Particle* particle = [Particle particleAt:self.center WithRep:rep Steps:25];
[particle setRadius:6];
[particle setVariant:arc4random()%AST_VARIATION_COUNT];
[particle setRotation: (arc4random()%100)/100.0*M_PI*2];
LinearMotion* motion = [LinearMotion linearMotionRandomDirectionAndSpeed];
[particle addBehavior:motion];
[controller addActor: particle];
}
[controller removeActor:self];
}
In Listing 7-20, we see the task doHit: . This task is called when we tap on the screen, causing all
Asteroids in the scene to break apart into a number of smaller Asteroids . We also want to generate
a number of asteroid-looking Particles when we do this. The first thing we do is to make sure
the Asteroid we call doHit: on is bigger than level one, because those asteroids should not create
additional Asteroids . If the Asteroid is bigger than level one, we create one to three new Asteroids
 
Search WWH ::




Custom Search