Game Development Reference
In-Depth Information
CGRect heatlhRect = CGRectMake(1, 1, (size.width-2)*percent, size.height-2);
CGContextFillRect(context, heatlhRect);
}
In Listing 12-17, we see the task
drawRect:
of class
HealthBarView
. In this task, we draw two
rectangles. The first rectangle is filled and the second is just an outline. Besides just drawing the
rectangles, we want to adjust the color of the rectangles to reflect how damaged the ship is. To
figure out which color we should use as our
baseColor
, we iterate through the
NSArray percents
until
the percent we are going to draw is higher than the percent at index.
Once we have our base color, we create two modified colors from it. The first is the color
frameColor
used for the outline of the health bar; it has an alpha of 0.8. The second color is
healthColor
and will
be used to draw the interior of the health bar; it has an alpha of 0.5.
CGRectMake
. Once the
CGRect
is created, we
CGContextStrokeRect
and
CGContextFillRect
, respectively.
BeltCommanderController
and know it works. In the next section, we
BeltCommanderController
handles the big picture. It controls how
Actors
are created, and it manages the interactions between them. However, a lot of the
Actors
. In this
Actors
and understand what the key customizations are
to make them work the way we want. In several earlier chapters, we have implemented Actors of
different types. In that light, we are only going to look at the bits of code that make each interesting
feature of the Actor work. If you would like a refresher on how to implement Actors, check out
Chapters 6 and 7. Also, you can always look at the full source code. Let's start with the class
Viper
.
The Viper Actor
The
Viper
class represents the spaceship at the left side of the screen. This is the
Actor
that the
user controls to achieve a high score. The
Viper
fires
Bullets
continuously to the right. To move the
Viper
, the user taps the screen, indicating a point above or below the
Viper
where it should travel.
When traveling, the graphic changes to show thrusters on the top or bottom of the craft. Also, a
Viper
can intercept a
Powerup
that improves its guns. Lastly, the
Viper
has a fixed amount of health
that slowly regenerates. In summary, the key features of the
Viper
are as follows:
Moves to a point specified by user.
Thrusters fire when ship moves.
Fires bullets continuously.
Improves quality of bullets when “powered up.”
Has a health value that regenerates.
Let's take a look at the source code and see how we implement these features, starting with the
header file for context, as shown in Listing 12-18.