Game Development Reference
In-Depth Information
Ending the level or game
The functions checkForEndLevel and checkForEndGame are called from the gameState
GameStates.STATE_CHECK_FOR_END . The level is over if the hit points for the computer
( computerLife ) are less than or equal to 0. Alternatively, the game is over if the players hit points
( playerLife ) is less than or equal to 0.
private function checkforEndLevel():Boolean { //changed
var retval:Boolean = false;
if (computerLife <= 0) {
dispatchEvent( new CustomEventSound(CustomEventSound.PLAY_SOUND,Main.SOUND_WIN,
false,1,0,1));
retval = true;
}
return retval;
}
private function checkForEndGame():Boolean { //new
var retval:Boolean = false;
if (playerLife <= 0) {
dispatchEvent( new CustomEventSound(CustomEventSound.PLAY_SOUND,Main.SOUND_LOSE,
false,1,0,1));
retval = true;
}
return retval;
}
Viewing the rest of the code for Dice Battle
We covered only the major changes from the Color Drop code to create Dice Battle in this
chapter. However, there are a lot of code changes necessary to create the game where we
simply renamed “block” to “die” so that it would be consistent for the game. Another good strategy
here would be to take the context out of these functions and make some generic names like
“objects” and create a drop-style game class that could be extended and reused for more games
of this type.
At any rate, here is the code that remains essentially the same from Color Drop, except for name
changes to existing variables and functions to match the content:
private function checkForFallingDice():Boolean {
var falling:Boolean = false;
for (var r:int = 0; r < board.length; r++) {
for (var c:int = 0; c < board[r].length; c++) {
tempDie =board[r][c];
if (tempDie != null) { //Changed Die Battle
if (tempDie.isFalling) {
falling = true;
}
}
}
}
return falling;
}
Search WWH ::




Custom Search