Game Development Reference
In-Depth Information
gameCenter
and the
ImageRepresentation rep
are passed to the super-initializer
initAt:WithRadi
us:AndRepresenation:
to create a
Saucer
object with a radius of 32. The object
saucer
is then set
as the delegate to the
ImageRepresenation
so we can specify the representational details with the
class
Saucer
. This works exactly like the actors in Chapter 6. Have a look at the source code to see
exactly how these tasks are implemented.
Once the
saucer
object is created, we want to initialize it with some details. We set which of the
three variants we want to use by calling
setVariant:
on
saucer
. We also set the max health and
current health properties to 100. Let's move on to the
HealthBar
class.
Instantiating the HealthBar Class
In order to render the health bar under the saucer, we create a
HealthBar
object and pass in the
saucer
. We also set the
healthBar
property of
saucer
to the
healthBar
so the
saucer
object can update the percentage displayed by
healthBar
healthBar
is added to the scene. Let's take a look at the constructor
HealthBar
and understand how it is set up and behaves the way it does. See Listing 7-4.
VectorRepresentation* rep = [VectorRepresentation vectorRepresentation];
HealthBar* healthBar = [[HealthBar alloc] initAt:anActor.center WithRadius:anActor.radius
[rep setDelegate:healthBar];
[healthBar setColor:[UIColor blueColor]];
[healthBar setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:.5]];
FollowActor* follow = [FollowActor followActor:anActor];
[follow setYOffset:[anActor radius]];
[healthBar addBehavior:follow];
return healthBar;
}
In Listing 7-4, we see the constructor task
healthBar:
and that it takes an
Actor
as an argument.
The
Actor anActor
is the actor that this
HealthBar
will stay under if
anActor
moves. In this example, the
Saucer
associated with it does not move, but we are going to use this class in future examples, where
its actor does move. Before we look at the behavior
FollowActor
, notice that
HealthBar
is initialized by
passing an instance of
VectorRepresentation
instead of an
ImageRepresentation
object. This indicates
that we want to draw this object programmatically. We will look at how this works in the next section.
For now, let's continue to explore this example. The next class we want to inspect is
FollowActor
.
The Behavior FollowActor Class
The class responsible for keeping the
HealthBar
near the
Saucer
is the class
FollowActor
. This class
is a
Behavior
and provides the same sort of abstraction we found with the other
Behavior
classes.
Let's see how all of this fits together by looking at the class
FollowActor
. See Listing 7-5.