Game Development Reference
In-Depth Information
Shooting the
BonusPlane
awards the player
10
shots for the Flak cannon (note that the
number of shots could be made into a difficulty setting).
There can only ever be as many
BonusPlane
objects as set in the
maxBonusPlanes
setting.
Besides these rules,
checkBonusPlane()
acts a lot like
CheckEnemies()
. First of all, we will use a
difficulty setting named
bonusPlaneDelay
. This setting tells us how long the game should wait
before a
BonusPlane
arrives. We do not want it to come too early or too often, or the player will
have an unfair advantage against the
Enemy
planes. The delay is calculated in frames. We use
bonusPlaneFrameCounter
to keep track of how frames have passed between
BonusPlane
arrivals.
private function checkBonusPlane():void {
bonusPlaneFrameCounter++;
if ( bonusPlaneFrameCounter >= bonusPlaneDelay) {
if (bonusPlaneArray.length < maxBonusPlanes) {
Now, we get to create an instance of
BonusPlane
. The first thing we will do is set the starting y
value for the plane. For this, we will simply get a random number between
0
and
bonusPlaneMaxY
.
This difficulty setting represents the highest y value (lowest place on the screen) that a
BonusPlane
can fly. The starting x value is
-32
, which means it will always start off the right side of
the screen, just far enough so the entire image is not visible. The ending
x
value is
gameWidth+32
,
wihich means the plane will fly until it is entirely off the right side of the screen. Both the starting
and ending y values are set to
randomY
because the y value will never change as the plane flies
acrosss the screen.
var randomY:int = Math.floor(Math.random()*bonusPlaneMaxY);
We also need to set the speed of the
BonusPlane
. The speed of the plane will increase on every
level. However, we do not want the plane to get too fast, or the player will not be able to shoot it
down. Because of this, we use the
bonusPlaneSpeedMax
difficulty setting. If the
BonusPlane
speed
is too fast, our code will set it to the maximum value.
The only thing left is to create an instance of
BonusPlane
and push it into
bonusPlaneArray
. Recall
that we give the player ten extra shots when a
BonusPlane
is shot down. The last parameter in the
call to
BonusPlane
is the literal
10
, which represents this value. The
BonusPlane
could support
different values for this bonus, but we are going to make it
10
every time. We then add it to the
display list with
addChild()
and reset the
bonusPlaneFrameCounter
so we can test for another
BonusPlane
when the proper number of frames have passed.
tempBonusPlane =new BonusPlane(-32,randomY,gameWidth+32,randomY,bonusPlaneSpeed,10);
bonusPlaneArray.push(tempBonusPlane)
this.addChild(tempBonusPlane);
} else {
bonusPlaneFrameCounter = 0;
}
}
}