Game Development Reference
In-Depth Information
Ending the game
We have gone through most of code now, and only a couple things are left to talk about. The first
is the function
testForEnd()
. This function is called by the
gameLoop()
to test for the level or
game end. First, it tests to see if the game has ended. We do this by checking
chances
to see if it
is great than
5
. If so, we set
gameState
to
STATE_END_GAME
, which will then call
endgame()
(described later in this section) the next time
gameLoop()
is called. If the game is not over, we test
to see if the
level
should be increased (thus making the game harder). We do this by testing the
score
to see if it is greater than
20 * level
. Since we give the player only one point per balloon, it
is easy to test when a level ends and another begins. If a player needs to pop
20
balloons per
level, we know to increase the
level
(
level++)
when that player has popped 20 balloons
multiplied by the number of the
level
(e.g.,
20
equals level 2, and
40
equals level 3).
public function testForEnd() {
if (chances >= 5) {
gameState = STATE_END_GAME;
} else if(score > level * 20) {
level ++;
levelText.text = level.toString();
}
}
Finally, endGame() is called when the gameSate is set to STATE_END_GAME. All this function
does is remove all the enemies from the screen and then stay static. You will need to relaunch
the game to play again. We will discuss ways to easily start games, end games, and transition
levels as this topic progresses.
public function endGame() {
for(var i:int = 0; i < enemies.length; i++) {
removeChild(enemies[i]);
enemies = [];
}
player.stopDrag();
}
So that's it. Balloon Saw is a pretty simple game. However, as an illustration of game loop, state
machine, and handler of multiple objects and user input, it's a pretty good starting point for your
further explorations into AS3 games.
Creating your third game: Pixel Shooter
Before we move on to creating a more efficient and complex game framework in Chapter 2, why
don't we try to create another type game based on Balloon Saw. We won't discuss this one in
quite the same detail as Balloon Saw; instead, we'll highlight the major differences and let you
explore the rest. This game changes up the Balloon Saw code just enough to create something
very different, but it's based on the same ideas. Because of that, it is a perfect illustration of
second game theory and the iterative process of making games.