Game Development Reference
In-Depth Information
Listing 7-22. Particle.m
@implementation Particle
@synthesize totalStepsAlive;
+(id)particleAt:(CGPoint)aCenter WithRep:(NSObject<Representation>*)rep Steps:(float)numStepsToLive{
Particle* particle = [[Particle alloc] initAt:aCenter WithRadius:32 AndRepresentation:rep];
[particle setTotalStepsAlive:numStepsToLive];
ExpireAfterTime* expire = [ExpireAfterTime expireAfter:numStepsToLive];
[expire setDelegate: particle];
[particle addBehavior:expire];
return particle;
}
-(void)stepsUpdated:(ExpireAfterTime*)expire In:(GameController*)controller{
self.alpha = [expire stepsRemaining]/totalStepsAlive;
}
@end
In Listing 7-22, we see that the constructor
particleAt:WithRep:Steps:
is pretty straightforward.
We create a new
Particle
object, passing in the provided
Representation
object. We also set the
property
totalStepsAlive
and create an
ExpiresAfterTime Behavior
. Notice that
particle
is set
as
expire
's
delegate;
this causes the task
stepsUpdated:In:
to be called every time
expire
is
executed.
In the task
stepsUpdated:In:
we simply adjust the
alpha
value of
Particle
based on how far along
Particle's
life cycle we are. In a more complex implementation of
Particle
, this fading behavior
would be configurable, not just on or off but also at which rate a
Particle
fades. In this simple
implementation, each particle simply fades in a linear way.
We have looked at the implementation of
Asteroid
and
Particle,
and see that it is pretty simple to
create a particle effect in our scene. In the next section, we will look at the Comet actor and see a
more visually striking example, though the implementation will be just as simple.
Creating Based Vector-Based Particles
In the previous example, we looked at
Particles
that were represented by a sequence of images.
Because we have a flexible way of rendering actors, we can just as easily create particles that are
programmatically drawn. In this example, we will be creating
Particles
that stay in one place, but
we will move the point where they are created. This will give our
Comet
actors a nice glowing tail, as
any comet should have. Figure
7-5
shows our new actor, the
Comet
.
c