Game Development Reference
In-Depth Information
Ending the game
The final function called by runGame() is checkForEndGame() . This function is very simple. It
checks to see if the ships variable is less than or equal to 0 ( <= 0 ). Why <= 0 and not just equal
to 0 ( ==0 )? Because, on the off chance that something goes wrong and the player's ships are
decremented past 0, (that should never happen, but just in case), the game would never end.
This simple code makes sure that bug never appears.
The rest of the code is pretty self-explanatory. First, we dispatch a Game.GAME_OVER event to the
game framework ( Main.as ) so it can start the end game sequence. Recall that the
Game.GAME_OVER (along with Game.NEW_LEVEL ) const was defined in the base Game class that we
extended for FlakCannon . We remove crosshairs from the screen with removeChild(crosshairs)
and make the mouse pointer appear again with Mouse.show(); . We remove the MOUSE_MOVE
EventListener that moves the now nonexistent crosshairs . We then call the cleanUpLevel()
function the same way we did in checkForEndLevel() to remove all the objects from the screen
and finally set the crosshairs to null so that they will be re-created when the game restarts.
private function checkforEndGame():void {
if (ships <=0) {
dispatchEvent(new Event(Game.GAME_OVER));
removeChild(crosshairs);
Mouse.show();
stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveListener);
cleanUpLevel();
crosshairs = null;
}
}
Finally, don't forget to finish-off your class with two concluding brackets after all the functions: one
to end the class, and to end the package:
}
}
Playing the game!
And there you have it. It's taken a very long time, but Flak Cannon has now been completed.
After you type in all this code (or download the source code and compile it), start playing Flak
Cannon. Enjoy a few rounds of the game; you deserve it!
As you play, note some things that you like and dislike about the game. While the game plays
pretty well, there are definitely ways to improve on the design and game play. Flak Cannon is
only a demonstration of how game like this might be created; it is not polished title ready for
publication.
Summary
Over the last two chapters, we have spent a lot of time discussing how to take the game
framework that we created in Chapters 2 and 3 and implement it by making a game based on
Atari's venerable coin-operated game, Missile Command. Our game was not an exact copy of
Missile Command by any means, but we used the ideas in that game as a jumping-off point to
Search WWH ::




Custom Search