Game Development Reference
In-Depth Information
destroying enemy planes as long as it stays on the screen. Then, we dispatch a
CustomEventSound
to play the
Main.SOUND_EXPLODE_PLANE
sound effect we created in Chapter 4.
removeItemFromArray(tempEnemy,enemyArray);
dispatchEvent( new CustomEventSound(CustomEventSound.PLAY_SOUND,
Main.SOUND_EXPLODE_PLANE,false,1,0,.5));
The next two lines of code are for functions that we will create a bit later in this section.
makeExplosions()
will create an instance of
Explosion
at the specified (x,y) location passed as
parameters to the function. Then, we call
addToScore()
passing the value that we want to add to
the player's score. The player gets the value of the setting
baseEnemyScore
points per enemy
destroyed (100 points by default), but we give a bonus of
enemyHitBonus
(500 points by default)
for every enemy a single flak explosion destroys that is greater than 1. So for instance, hitting a
single enemy with a flak explosion would net the player 100 points. However, hitting two enemy
planes with the same flak explosion would score 700 points (100
2 + 500
1); hitting three
enemy planes would score 1,300 points (100
3 + 500
2).
makeExplosion(tempEnemy.x,tempEnemy.y);
addToScore(baseEnemyScore+(tempFlak.hits*enemyHitBonus));
We then update the
hits
property of the current
Flak
explosion so we can give the player a
bonus if more than one enemy is indeed hit. We need
hits
to be a property of
Flak
, not a local
variable to the loop, because a
Flak
explosion stays around for multiple frames, which means
multiple runs through this collision detection function. For that reason,
hits
needs to persist
beyond the scope of this single call to
checkCollisions().
tempFlak.hits++;
Finally, we will make use of our label. The
break
statement will break us out of the current loop
back to the enemy label. This essentially gets us out of testing the
Enemy
that was just destroyed
against any more
Flak
explosions. We start with a new
Enemy
, and test all the
Flak
explosions
again. Again, we need to do this so that the Flash runtime does not throw errors resulting from a
null
reference to an
Enemy
object that no longer exists (because it just blew up).
break enemy;
}
}
}
Now, we will test the
BonusPlane
objects against the
Flak
explosions. Because we had to test the
Flak
explosions in a nested loop inside
Enemy
when testing
Enemy
and
Flak
collisions, we now
have to loop again through the
Flak
explosions. However, since there is not always a
BonusPlane
on the screen, there is a good chance that this check will not happen on any given frame.
The
bitmapData.hitTest()
test for the
BonusPlane
objects is nearly identical to the one we used
for the
Enemy
objects, as is much of the code here. We play a different sound if the
BonusPlane
is
hit
(Main.SOUND_BONUS
), and we add the value of
tempBonusPlane.bonusvalue
to the player's
shots
counts. We then dispatch an event to the scoreboard to tell it that
shots
has been updated.
Another small difference is that we add
bonusPlaneScore
to the player's score instead of
baseEnemyScore
.
Also, notice that we want our
Explosion
to be directly over the
BonusPlane
. However, since the
plane is 64
32 pixels, we need to move it over by half
its width and a quarter of its height to center it over the spot that the plane had been when it was
destroyed.
64 pixels and the explosion is only 32