Game Development Reference
In-Depth Information
Checking for enemies
The first thing we do in our
runGame()
function is call
checkEnemies()
. This function is designed to
work with the
enemyWaveDelay
difficulty setting and the
enemyFrameCounter
variable. Its job is to
test to see if a new enemy plane should be created to attack the player's fleet.
The first few lines of code set up the function and then test to see if we need to create an enemy.
First, we increment
enemyFrameCounter
and test it against
enemyWaveDelay
. On the first level,
enemyWaveDelay
is set to
60
. That means that
60
frames need to fire before an enemy is created.
We also test
incomingCount
against
numEnemies
.
incomingCount
is incremented each time an
enemy is created.
numEnemies
is another difficulty settings that represents the number of enemies
per level. On the first level, this is set to
15
.
private function checkEnemies():void {
enemyFrameCounter++;
if ((enemyFrameCounter >= enemyWaveDelay) && (incomingCount < numEnemies)) {
Now, we need to test to see if we are going to create a wave of multiple enemies, and if so, how
many enemies we will create. For this, we will test the
enemyWaveMultipleChance
difficulty setting
against a percentage chance random number (1-100). If the random number (
chance
) is less
than or equal to
enemyWaveMultipleChnace
, the current wave we are creating will have multiple
enemies at the same time. The more enemy planes that arrive at the same time, the harder it will
be for the player to shoot them all down.
var chance:int = Math.floor(Math.random() * 100)+1;
var enemiesToCreate:int = 1;
Figure 5-4 shows what the screen will look like when
Enemy
planes start appearing in timed
intervals.
Figure 5-4. Single enemies flying down
If we do have multiple enemies, as shown in Figure 5-5, we will then find out how many by
creating a random number with the maximum value using another difficulty setting,
enemyWaveMax
.
We place that value into the local
enemiesToCreate
variable.