Game Development Reference
In-Depth Information
Updating the render function for NoTanks.as
The
render
function needs to be altered to add in a set of iterations through both the player and
enemy missile arrays. Add the following code to the
render
function below the existing code but,
of course, before the ending bracket for the
render
function:
//** added in final Game.as
var playerMissileLength:int = playerMissileList.length-1;
for (ctr = playerMissileLength; ctr >= 0; ctr--) {
tempMissile = playerMissileList[ctr];
tempMissile.x = tempMissile.nextX;
tempMissile.y = tempMissile.nextY;
}
var enemyMissileLength:int = enemyMissileList.length - 1;
for (ctr = enemyMissileLength; ctr >= 0; ctr--) {
tempMissile = enemyMissileList[ctr];
tempMissile.x = tempMissile.nextX;
tempMissile.y = tempMissile.nextY;
}
//** end added in final Game.as
Checking for the end of a level or game
We now need to add three brand new functions, as shown in the following code.
The
checkForEndOfLevel
function simply checks to see if the
goalReached
Boolean is set to
true
.
The
checkForEndOfGame
function checks for the
gameOver
Boolean to be
true
. It also checks to
see if all of the explosions have finished. This is just added as an example of how to make
transitions between games less abrupt. You will see that the levels end slightly abrupt manner,
but the game-over sequence is a little smoother. You can add as many conditions you want to in
the two functions to make the transition as smooth as you like.
The
addToScore
function takes in an integer value that is applied to the
score
.
private function checkforEndGame():void {
if (gameOver && explosionList.length==0) {
dispatchEvent(new Event(GAME_OVER));
disposeAll();
}
}
private function checkforEndLevel():void {
if (goalReached) {
disposeAll();
dispatchEvent(new Event(NEW_LEVEL));
}