HTML and CSS Reference
In-Depth Information
var currentGameState = 0;
var currentGameStateFunction = null;
We will create a function called switchAppState() that will be called only when we want
to switch to a new state:
function switchGameState(newState) {
currentGameState = newState;
switch (currentState) {
case GAME_STATE_TITLE:
currentGameStateFunction = gameStateTitle;
break;
case GAME_STATE_PLAY_LEVEL:
currentGameStateFunctionappStatePlayeLevel;
break;
case GAME_STATE_GAME_OVER:
currentGameStateFunction = gameStateGameOver;
break;
}
}
We will call the runGame() function repeatedly in the setInterval() method. run
Game() will call the currentGameStateFunction reference variable on each frame tick.
This allows us to easily change the function called by runGame() based on changes in
the application state:
setInterval(runGame, intervalTime );
function runGame(){
currentGameStateFunction();
}
Let's look at the complete code. We will create some shell functions for the various
application state functions. Before the application starts, we will call the switchGame
State() function, and pass in the constant value for the new function we want as the
currentGameStateFunction :
//*** application start
switchGameState(GAME_STATE_TITLE);
In Example 8-9 , we will use the GAME_STATE_TITLE state to draw a simple title screen
that will be redrawn on each frame tick.
Example 8-9. The tile screen state
<script type="text/javascript">
window.addEventListener('load', eventWindowLoaded, false);
function eventWindowLoaded() {
canvasApp();
Search WWH ::




Custom Search