Game Development Reference
In-Depth Information
if (stepsRemaining <= 0){
[gameController removeActor:anActor];
}
}
@end
In this implementation of the class
ExpireAfterTime
, the constructor
expireAfter
: simply creates a
new
ExpireAfterTime
object and sets the
stepsRemaining
property. The task
applyToActor:In
:
is called by a
GameController
for each step of the game. In this task, we decrement
stepsRemaining
and update the delegate that the number of steps was updated. If
stepsRemaining
is zero or less,
we remove the actor from the
GameController
.
Before we add
ExpireAfterTime
to the
Powerup
object, we set the
Powerup
object as the delegate
ExpireAfterTime
. This causes
ExpireAfterTime
to call
stepsUpdated:In
: on the
Powerup
object.
stepsUpdated:In
: for the class
Powerup
.
Powerup.m (stepsUpdated:In:)
long stepsRemaining = [expire stepsRemaining];
if (stepsRemaining<60*5){
if (stepsRemaining % 25 == 0){
if (self.state == STATE_GLOW){
self.state = STATE_NO_GLOW;
} else {
self.state = STATE_GLOW;
}
}
}
}
In this listing, we check how many steps are remaining for the
ExpireAfterTime
object. If that value
is fewer than 300 steps (5 seconds at 60 frames per second), we want to apply our blinking logic. To
make the power-up blink, we swap the state of
Powerup
from STATE_GLOW to STATE_NO_GLOW, or
vice versa.
Changing the state is all that is required to change which image represents the
Powerup
actor
because (looking back at Listing 6-10) we know that setting the state of an actor sets its
needViewUpdated
property to YES. Further, if an actor's
needViewUpdated
property is YES and that
actor is using an
ImageRepresentation
, that
ImageRepresentation
will fetch the correct image for the
actor in the task
updateView:In
: (as shown earlier in Listing 6-19).
Summary
In this chapter, we looked at our base game engine classes
GameController
and
Actor
and learned
how to use these classes to create a simple scene exercising our new actor, the power-up. We
looked at the protocol Behavior, which provides us with a framework for creating reusable bits of
game logic to be applied to our actors. We also consolidated the logic required to render an image-
based actor into the class
ImageRepresentation
. This also set the stage for vector-based actors,
which will be discussed in the following chapter.