Game Development Reference
In-Depth Information
The runGame function definition
The runGame function is the core of the state machine. Once the systemFunction has been set
with the switchSystemState (discussed next) function call, the runGame function will call it
repeatedly at the set timerPeriod rate every 33 milliseconds (or so) for or frame rate of 30.
public function runGame(e:TimerEvent):void {
systemFunction();
e.updateAfterEvent();
}
The e.updateAfterEvent() function call tells the Flash player to make an extra screen update
after the frame tick is over, rather than waiting for the next system frame update to occur. System
frame update events happen based on the SWF's stage frame rate. If we don't call
e.updateAfterEvent here, the screen would not be updated until an actual system frame update
event occurs. By using this, we smooth out the look of the screen updates to coincide with out
selected gameTimer delay value.
The switchSystemState function definition
While the runGame function is the core of the timer, the switchSystemState() function is the core
of the simplified state machine. It is passed a constant value for the state. Using that value, it
switches the systemFunction reference accordingly.
The switchSystemState function is used to change the current systemFunction of the timer for the
next frame timer tick. A reference to one of the state constants is passed into the function, and it
acts on it to change the systemState variable. It also changes the lastSystemState and
nextSystemState variables. As a refresher, here are the constants from the variable definition
section of this FrameWorkStates.as class:
public static const STATE_SYSTEM_WAIT_FOR_CLOSE:int = 0;
public static const STATE_SYSTEM_TITLE:int = 1;
public static const STATE_SYSTEM_INSTRUCTIONS:int = 2;
public static const STATE_SYSTEM_NEW_GAME:int = 3;
public static const STATE_SYSTEM_GAME_OVER:int = 4;
public static const STATE_SYSTEM_NEW_LEVEL:int = 5;
public static const STATE_SYSTEM_LEVEL_IN:int = 6;
public static const STATE_SYSTEM_GAME_PLAY:int = 7;
public static const STATE_SYSTEM_LEVEL_OUT:int = 8;
public static const STATE_SYSTEM_WAIT:int = 9;
We first set lastSystemState = currentSystemState , so we can have a reference if needed to
switch back to previous state. This might occur in circumstances where we need to jump to the
STATE_SYSTEM_WAIT state for a period of time and then jump back to the state we were in before
the wait. The systemLevelIn function is a good example of this. Once we get to the
systemLevelIn function, we want to wait a specified number of milliseconds before removing the
levelInScreen from the display. Once the wait is over, the WAIT_COMPLETE event is fired off. The
waitCompleteListener function will need to know what the previous systemState was before the
wait so it can determine what to do next.
We then set currentSystemState = stateval . The stateval was passed when we called the
switchSytemState function. This forces the switch/case statement to set the current
systemFunction to the function we want to repeatedly call in our loop. We will now start with the
first function state the loop calls, the systemTitle function.
Search WWH ::




Custom Search