Game Development Reference
In-Depth Information
The last thing the
updateScene
task does is check to see if any Bullet has collided with the Saucer.
This is done by calling
actorsOfType:
and getting all
Bullet
objects in the scene stored in the
NSMutableSet
*
bullets
. We simply iterate over each
Bullet
and
Saucer
and check if there is a hit by
calling
overlapsWith:
. If there is a hit, we call the
doHit:with:
task of the class
Saucer
.
Let's take a closer look at them implementation of the
Actor
classes used in this example.
The Actor Classes
In this section, we learn about the
Saucer
actor and the
HealthBar actor
classes. The
HealthBar
is
different from other
actors
we have looked at up until this point. It is different because its location
in the scene is dependent on the location of another
Actor—
in this case, a
Saucer
. Once we have
reviewed the classes
Saucer
and
HealthBar
, we will look at the
Behavior
class
FollowActor
and see
how we implement this feature.
Instantiating the Saucer Class
We will start by looking at the constructor for the class
Saucer
. We want to start with the
Saucer
's
constructor because this is where we create the
HealthBar
. In this way, we don't have to worry about
adding a
HealthBar
when we create a new
Saucer
because one will be created automatically every
time we create a
Saucer
. The constructor for
Saucer
is shown in Listing 7-3.
Listing 7-3. Saucer.m (saucer:)
+(id)saucer:(GameController*)controller{
CGSize gameAreaSize = [controller gameAreaSize];
CGPoint gameCenter = CGPointMake(gameAreaSize.width/2.0, gameAreaSize.height/2.0);
ImageRepresentation* rep = [ImageRepresentation imageRep];
[rep setBackwards:arc4random()%2 == 0];
[rep setStepsPerFrame:3];
Saucer* saucer = [[Saucer alloc] initAt:gameCenter WithRadius:32 AndRepresentation:rep];
[rep setDelegate:saucer];
[saucer setVariant:arc4random()%VARIATION_COUNT];
[saucer setMaxHealth:100];
[saucer setCurrentHealth:100];
HealthBar* healthBar = [HealthBar healthBar:saucer];
[healthBar setPercent:1];
[saucer setHealthBar:healthBar];
[controller addActor:healthBar];
return saucer;
}
In Listing 7-3, we start by finding the center of the game area and storing that value in the
CGPoint
gameCenter
. Next, we create an
ImageRepresentation
called
rep
. We indicate that
rep
should
spin the
Saucer
backward half the time at a rate of 3 steps per frame of animation. The
CGRect