Game Development Reference
In-Depth Information
if (chance <= enemySideChance) {
var leftOrRight:int = Math.floor(Math.random() * 2);
startY
is the starting
y
position of the
Enemy
plane. We have to incorporate another difficulty
setting to calculate this value,
enemySideFloor
.
enemySideFloor
is the lowest point at which an
Enemy
plane will arrive on the screen from the side. The idea here is that the lower an
Enemy
enters the screen, the harder it will be to shoot down. The ending y value (
endY
) is always
gameHeight
, which means it will exit at the bottom of the screen. We don't need to find an
endX
value, because
Enemy
tests only the
endY
value to see if the
Enemy
has finished flying.
Now, depending on if the
Enemy
is flying to the left or right, we will create some slightly different
values. If the plane is flying left, it will start at the extreme right of the screen. For that reason,
startX
equals
gameWidth
. If the plane is flying to the right, it will start at the extreme left (0) minus
the width of the
Enemy
(-32). If you recall from Chapter 4, the
Enemy
class will select an angle for
the
Enemy
to travel based on the direction (
dir
). Flying left to the left, the angle is 135 degrees
(see Figure 5-6); from the right, it is 45 degrees.
startY = Math.floor(Math.random() * enemySideFloor)+1;
endY = gameHeight;
switch(leftOrRight) {
case 0: //left
startX = gameWidth;
dir = Enemy.DIR_LEFT;
break;
case 1: //right
startX = -32;
dir = Enemy.DIR_RIGHT;
break;
}
Figure 5-6. Enemy coming in from the left at a 135-degree angle
For
Enemy
planes that fly down (
Enemy.DIR_DOWN
), the values are quite easy to calculate. The
starting x value (
startX
) is a random number generated that is the width of the screen
(
gameWidth
) minus the width of the
Enemy
plane (32). This will keep the
Enemy
planes within the