Game Development Reference
In-Depth Information
//newLevelListener listens for Game.NEWLEVEL
//simple custom events calls and changes state accordingly
public function newLevelListener(e:Event):void {
switchSystemState(FrameWorkStates.STATE_SYSTEM_NEW_LEVEL);
}
public function waitCompleteListener(e:Event):void {
switch(lastSystemState) {
case FrameWorkStates.STATE_SYSTEM_LEVEL_IN:
removeChild(levelInScreen);
break
}
removeEventListener(EVENT_WAIT_COMPLETE,waitCompleteListener);
switchSystemState(nextSystemState);
}
}
}
The class imports
The class import section contains the necessary Flash core classes needed for the
Main
class.
Notice the package name coincides with the package structure we created earlier in the chapter
for the framework:
package com.efg.framework
{
We also must import all of the classes needed for the framework to run. You will see this put to
use shortly.
The variable definitions
The variable definition section defines all of the global scope variables for the class. These
include all of the variables needed for the state machine, screens, and the game timer.
We will make use of constants to define the current state and a set of variables to hold the state
information. These have been be defined on the
FrameWorkStates.as
file we created in the last
section. More states can be added to the basic ones, but these will be sufficient for many games
that we will create in this topic. There are two special states that are used for the system and wait
for button clicks or things like animations to complete. These are the
STATE_SYSTEM_WAIT_FOR_CLOSE
and
STATE_SYSTEM_WAIT
respectively. We will also make use of a
generic function called
systemFunction
that will hold the current state function to call in our game
loop. Combined with this, we use a set of integer variables to hold the value of the current state
(
currentSystemState
), the last state (
lastSystemState
) and the next state (
nextSystemState
) for
processing purposes. These states should not be confused with an actual game pause function.
This will be handled in a different manner and added to the framework in Chapter 11.
If you are using the Flash IDE and have any assets in the library that need to be exported in the
first frame, you must extend
MovieClip
and not
Sprite
even if you don't plan to use the main time
line for anything else. We have extended
MovieClip
for the
GameFrameWork
so it will work with both
Flex SDK and Flash IDE projects.