Game Development Reference
In-Depth Information
We are finished with the
checkCollisions()
function, but we still need to create a few functions to
support the code we just added. The first function is
createExplosions()
. This function accepts
the (x,y) coordinates to place an instance of the
Explosion
object. Recall the
Explosion
class that
we created in Chapter 4. It displays multiple frames of animation to simulate an explosion.
Creating the explosion and putting it on the screen is a very straightforward operation. We have
used similar code several times previously in Flak Cannon. Here is the full code for the function.
private function makeExplosion(explodeX:int,explodeY:int):void{
tempExplode = new Explosion();
tempExplode.x = explodeX;
tempExplode.y = explodeY;
this.addChild(tempExplode);
explodeArray.push(tempExplode);
}
The
addToScore()
function is called whenever the player gains points by destroying
Enemy
planes.
addToScore()
accepts the value to add to the player's score as its only parameter. The
score
is
updated, and an event is dispatched to tell the
ScoreBoard
to display a new
score
value. We also
call
checkBonusShips()
. This function, described a bit further on in this chapter is used to check to
see if the player has attained a score that will net an extra
Ship
for the fleet.
private function addToScore(val:Number):void {
score += int(val);
checkBonusShips();
dispatchEvent(new CustomEventScoreBoardUpdate(
CustomEventScoreBoardUpdate.UPDATE_TEXT,Main.SCORE_BOARD_SCORE,String(score)));
}
The
checkBonusShips()
function does a simple arithmetic test to see if the player has attained a
score that will award an extra
Ship
. However, there is a trick to it that may not seem evident at
first. We have created a difficulty setting named
scoreNeededForExtraShip
. For this game,
scoreNeededForExtraShip
is
10000
, but it could be any score. It might seem logical that we would
simply check to see if the player's score is equal to some multiple of
scoreNeededForExtraShip
(e.g.,
10000
,
20000
, and
30000
), and if so, award an extra
Ship
. However, we cannot be sure that
the player's score will ever actually be equal to multiple of
scoreNeededForExtraShip
, so there is
a good chance we will never award the player an extra
Ship
if we simply look at this value.
Alternatively, if we look for a
score
that is greater-than-or-equal-to
scoreNeededForExtraShip
, we
will continue to award the player an extra
Ship
every time we call this function, after the first
multiple of
scoreNeededForExtraShip
is reached. Instead, we must use a combination of both
checks to award the proper amount of bonus
Ship
objects to the player. Here is the calculation:
if ( (score-(extraShipCount*scoreNeededForExtraShip) >= scoreNeededForExtraShip) ) {
For this example, let's pretend the player has a score of 20,100 and has earned one bonus ship
already in the game. Here it is translated into a more readable form:
If 20100-(1*10000) >= 10000
We could write this even more simply as follows:
If 10100 >= 10000
Since this statement is true, the player gets an extra
Ship
. However, the next time this calculation
is called, called it would be like so:
If 20100-(2*10000) >= 10000