Game Development Reference
In-Depth Information
Now that we can see how each particle is drawn, we should look how the particles are created,
Listing 7-26 shows the implementation of
Comet's step:
task.
Listing 7-26. Comet.m (step:)
-(void)step:(GameController*)controller{
if ([controller stepNumber]%3 == 0){
VectorRepresentation* rep = [VectorRepresentation vectorRepresentation];
[rep setDelegate:self];
int totalStepsAlive = arc4random()%60 + 60;
Particle* particle = [Particle particleAt:self.center WithRep:rep Steps:totalStepsAlive];
[particle setRadius:self.radius];
[controller addActor: particle];
}
}
In Listing 7-26, the step: task creates a new
Particle
every three steps in the animation. For each
new
Particle
, a
VectorRepresentation
is created, but note that the
delegate
is the
Comet
object.
The
Particle
is created with a random life span from 1 to 2 seconds. This randomness creates the
slight flickering variation in the tail of the comet. Notice that the
Particle
has no additional behaviors
added at this point; this causes the
Particle
to just sit in one place and fade away.
Summary
In this chapter we looked at a technique to create actors in our game that are drawn
programmatically. We created a new class called
VectorRepresentation
to handle the details of
creating a
UIView
for our code to draw on. We used these new vector-based actors to create a
health bar and a simple bullet. The chapter continued by showing two simple examples of creating
particle systems. The first example reused the art for the asteroids to create a sense of debris when
we exploded the asteroids. The second example used vector-based particles to create comets with
glowing tails. The actors created in this and the previous chapter will be used to create a complete
game in Chapter 12.