Game Development Reference
In-Depth Information
if (chance <= enemyWaveMultipleChance) {
enemiesToCreate = Math.floor(Math.random() * enemyWaveMax)+1;
}
Figure 5-5. Multiple enemies flying down in a single wave
We then adjust the wave size to the number of enemies we have left on the current level (just in
case the wave is bigger than how many are left). Recall,
enemiesToCreate
represents the number
of
Enemy
objects that we need to create.
if (enemiesToCreate > (numEnemies-incomingCount)) {
enemiesToCreate = (numEnemies-incomingCount);
}
Now, it is time to create the
Enemy
planes and put them on the screen. We will do this in a loop,
with
enemiesToCreate
being the maximum number of iterations. If you recall, in Chapter 4, the
Enemy
class we created required a starting and ending point to define the vector on which the
plane would move. This is where we will calculate those points. We start this by initializing
startX
and
startY
(the starting x and y values),
endX
and
endY
(the ending x and y values) plus
dir
,
which represents the direction the plane will fly (
Enemy.DIR_DOWN
,
Enemy.DIR_RIGHT
, or
Enemy.DIR_LEFT
).
for (var ctr:int = 0; ctr < enemiesToCreate; ctr++) {
var startX:int = 0;
var startY:int = 0;
var endX:int = 0;
var endY:int = 0;
var dir:int = 0;
chance = Math.floor(Math.random() * 100)+1;
Now, we will use still another difficulty setting,
enemySideChance
, to test to see which direction this
plane will be flying. We test against a percentage chance, just like we did with
enemyWaveMultipleChance
. If this plane does indeed come from the side, we will do another
random test to see if the plane will come in from the left or right (
leftOrRight
).