Game Development Reference
In-Depth Information
Finally, the endgame() function is called by the gameLoop() when the gameState is set to
STATE_GAME_OVER by playGame(). Just to get use to the process, we call
removeEventListener() for the MouseEvent.CLICK event. We do this because event listeners
should be cleaned up when a game is finished. We then set the gameState to STATE_INIT, so
the game will automatically start over, and we trace out the text game over just to tell the player
what is happening.
public function gameOver():void {
stage.removeEventListener(MouseEvent.CLICK, onMouseClickEvent);
gameState = STATE_INIT;
trace("game over");
}
Now, we will put all of this code together to make the very simple game. The code we have just
described will all be collected into a class named Game. This code can be compiled with Flex or
dropped directly into the Flash IDE as the document class (the main class that will be executed
when a program is run) for an empty AS3 application.
Here is simple model of how this program will flow:
1.
The
Game
class is instantiated.
2.
The game constructor is called.
3.
The game constructor sets up the game timer to be called every frame.
4.
The game constructor sets the
gameState
to
STATE_INIT
to set up the game.
5.
The game timer calls
gameLoop()
after the next frame is fired in Flash or Flex.
6.
gameLoop()
decides which function to call based on the
gameState
variable.
7.
initGame()
is called to set up the game. The event listener for the mouse is created,
and variables are reset.
8.
initGame()
sets the
gameState
to
STATE_PLAY
.
9.
The game timer
calls
gameLoop()
after the next frame is fired in Flash or Flex.
10.
gameLoop()
decides which function to call based on the
gameState
variable.
11.
playGame()
called to check the game over condition (
clicks >= 10
).
These steps will go on forever unless the
MouseEvent.Click
event is observed from (you guessed
it) someone clicking the mouse. Once that happens, this is the flow:
12.
If
clicks
is greater than 10,
playGame()
changes
gameState
to
STATE_GAME_OVER
.
13.
The game constructor sets the
gameState
to
STATE_INIT
to set up the game.
14.
The game timer calls
gameLoop()
after the next frame is fired in Flash or Flex.
15.
gameLoop()
decides which function to call based on the
gameState
variable.
16.
gameOver()
is called to clean up the game and start over.