HTML and CSS Reference
In-Depth Information
The Game State Machine
A state machine is a programming construct that allows for our game to be in only a single
application state at any one time. We will create a state machine for our game, called applica-
tionstate ,whichwillincludesevenbasicstates(wewilluseconstantstorefertothesestates):
GAME_STATE_TITLE
GAME_STATE_NEW_GAME
GAME_STATE_NEW_LEVEL
GAME_STATE_PLAYER_START
GAME_STATE_PLAY_LEVEL
GAME_STATE_PLAYER_DIE
GAME_STATE_GAME_OVER
We will create a function object for each state that will contain game logic necessary for the
state to function and to change to a new state when appropriate. By doing this, we can use
the same structure for each game we create by simply changing out the content of each state
function (as we will refer to them).
Let's take a look at a very basic version of this in action. We will use a function reference
variable called currentGameStateFunction , as well as an integer variable called cur-
rentGameState that will hold the current application state constant value:
var
var currentGameState = 0 ;
var
var currentGameStateFunction = null
null ;
We will create a function called switchAppState() that will be called only when we want to
switch to a new state:
function
function switchGameState ( newState ) {
currentGameState = newState ;
switch
switch ( currentGameState ) {
case
case GAME_STATE_TITLE :
currentGameStateFunction = gameStateTitle ;
break
break ;
case
case GAME_STATE_PLAY_LEVEL :
currentGameStateFunctionappStatePlayeLevel ;
break
break ;
Search WWH ::




Custom Search