Game Development Reference
In-Depth Information
at the same location as the current Asteroid with one level smaller. These new Asteroids will travel
away from the current Asteroid in a straight line until they also explode.
To create the particle effect in Listing 7-20, we first decide how many particles we will be adding
to the scene. We then create a new Particle using the Asteroids current position, the singleton
instance of AsteroidRepresentationDelegate , and indicate that the particle should live for 25 steps
of animation. The radius of particle is set to 6. This is a purely aesthetic choice; it could be any
value. The variation and rotation properties are also set to provide visual variation in each particle
that is created. The last thing we do to the particle is to specify a random LinearMotion before
adding it to the scene. Figure 7-4 shows a close-up of the new Asteroids being created, as well as
the Particles .
Asteroids and particles, zoomed in
The Particle Class
We looked at how the Particles are created when the each Asteroid explodes, so let's now look
at the Particle class in detail and see how it is implemented, starting with the header file shown in
Listing 7-21.
Listing 7-21. Particle.h
@interface Particle : Actor<ExpireAfterTimeDelegate> {
}
@property (nonatomic) float totalStepsAlive;
+(id)particleAt:(CGPoint)aCenter WithRep:(NSObject<Representation>*)rep Steps:(float)numStepsToLive;
@end
In Listing 7-21, the header for the class Particle extends Actor and conforms to the protocol
ExpireAfterTimeDelegate. Particle conforms to ExpireAfterTimeDelegate, because we want to
change the opacity of the particle, fading it out, during its lifetime. The property totalStepsAlive
indicates how long the particle should exists in the scene, and is set as part of the constructor
particleAt:WithRep:Steps:. We can also see that a particle requires a Representation to be passed as
an argument to the constructor. The implementation of Particle is shown in Listing 7-22.
 
Search WWH ::




Custom Search