Game Development Reference
In-Depth Information
There are a couple small differences to the constructor as well. Instead of having a
speed
set in
the class, we pass in a
speed
parameter from
FlakCannon
. This will allow
Game
to use its
enemySpeed
difficulty setting to set the speed of the
Enemy
planes as levels progress. We also pass
in a value for
dir
(one of the static const values). This allows
Game
to use its
enemySideChance
difficulty setting as the levels progress.
public function Enemy(startX:Number, startY:Number, endY:Number, speed:Number,
dir:int) {
startLocation = new Point(startX,startY);
endLocation = new Point(0,endY);
nextLocation = new Point(0,0);
this.dir = dir;
this.speed=speed;
init();
}
Finally, we need to do something with the
dir
instance variable that defines the direction that the
plane will fly on the screen. This happens in
init()
We use a
switch()
statement to decide
which
Bitmap
to attach to the class depending on the value of
dir
. Notice again, that there is
different code here for Flash and Flex. However, there is a much more important difference in this
function than in the one we created for
BonusPlane
and
Shot
. Instead of using already calculated
xunits
and
yunits
, this function uses
angle
and
speed
values to resolve the vector that this object
is moving on. This is a much more flexible way to handle vectors than point-to-point because you
can calculate movement simply by changing the angle of an object.
To start, let's discuss how Flash views the angles. Flash uses a Cartesian coordinate system
(basically a grid with x and y axes) to position objects on a 2D plane. We have already used this
system many times by setting the x an y values of various sprites and
MovieClip
s. However,
when using angles, it is important to know how this system works in a bit more detail.
Angles in Flash are calculated with the origin of 0 degrees pointing directly to the right of the
screen (as shown in Figure 4-15). This is because of the Cartesian coordinate system.
Figure 4-15.
How Flash AS3 calculates angles