Game Development Reference
In-Depth Information
p.gameStateGame = function () {
var scene = new game.Game();
scene.on(game.GameStateEvents.GAME_OVER, this.onStateEvent, this,
false, {state:game.GameStates.GAME_OVER});
stage.addChild(scene);
stage.removeChild(this.currentScene);
this.currentScene = scene;
this.changeState(game.GameStates.RUN_SCENE);
}
p.gameStateGameOver = function () {
var scene = new game.GameOver();
stage.addChild(scene);
scene.on(game.GameStateEvents.MAIN_MENU, this.onStateEvent, this,
false, {state:game.GameStates.MAIN_MENU});
scene.on(game.GameStateEvents.GAME, this.onStateEvent, this,
false, {state:game.GameStates.GAME});
stage.removeChild(this.currentScene);
this.currentScene = scene;
this.changeState(game.GameStates.RUN_SCENE);
}
p.gameStateRunScene = function (tickEvent) {
if (this.currentScene.run) {
this.currentScene.run(tickEvent);
}
}
Most of these functions share a pretty common pattern. For this exercise, with the exception of
gameStateRunScene , the major task of each state function is to create new scenes and dispose of the old. After creating
each new scene, a listener is set on them to notify the application that the current state should change. Each of these
functions ultimately changes the state to RUN_SCENE when it's finished with its scene management tasks. This state will
run the newly created scene on each subsequent game tick.
You will notice that the majority of these game state functions only last for one tick, so why bother making them
states at all? In this exercise, the state machine is pretty simple, so a simple, repeating pattern is recognized. However,
as your games start to grow and involve more states, these state functions will likely have a much larger responsibility.
Finally, the heartbeat of the game, and the state machine itself, is run from each game tick in the onTick handler
that is set up on the Ticker , seen in Listing 10-19.
Listing 10-19. OrbDestroyer.js - The Tick Handler Runs the State Machine
p.onTick = function (e) {
if (this.currentGameStateFunction != null) {
this.currentGameStateFunction(e);
}
stage.update();
}
Although the game itself is extremely simple, this exercise outlines the state machine template that will be used
for the remaining games in this topic. You should have a good understanding of how more states could easily be
added by adjusting the parts that make up the state machine.
 
Search WWH ::




Custom Search