Game Development Reference
In-Depth Information
We have reviewed the class
ImageRepresentation
and how it is used to coordinate the various states
of the actor with the image intended to represent it. The goal in designing this class was to make it
easy to facilitate creating new
Actor
classes that use it. In the other examples in the chapter, we will
be reusing this class in various configurations and we will see how it handles these other cases.
The last thing to understand about this example is how the power-ups move across the screen and
how they change state to create the blinking effect, as discussed in the following section.
Understanding Behaviors by Example
In this example, we have power-ups tumbling across the screen. Eventually they start blinking, just
before they vanish. These two distinct behaviors could simply be implemented by providing the
Powerup
. In the previous chapter, this is how we implemented the different
Powerup's
constructor that dealt with
Powerup.m (powerup: (partial))
[powerup addBehavior: motion];
ExpireAfterTime* expire = [ExpireAfterTime expireAfter:60*30];
[expire setDelegate: powerup];
[powerup addBehavior: expire];
return powerup;
We have just created a new
Powerup
object called
powerup
and set a few basic properties. The next
step is to create Behavior objects and add them to the newly created
Powerup
before returning the
objects. The first behavior we create is a
LinearMotion
object called motion, which we add to the
powerup
with the task
addBehavior
:. The second behavior is an
ExpireAfterTime
object called expire.
Before adding expire to
powerup
, we register
powerup
as a delegate to expire. This gives us an easy
way to make the power-up blink when it gets close to its expiration.
Behavior: Linear Motion
Both of these behaviors will be reused with other
Actor
classes, so they are worth looking at in some
detail. Let's start with the class
LinearMotion
, whose header is shown in Listing 6-22.
Listing 6-22. LinearMotion.h
#import <Foundation/Foundation.h>
#import "Actor.h"