Game Development Reference
In-Depth Information
Implementing the pause and mute functionality
We have discussed the pause and mute functions previously, but in the
Main.as
class, we now
must change the
init
function and add in an override of the
addedToStage
functionality. These
are necessary so we can add key listeners for the pause and mute functionality after we know the
Flash stage is available. One note, you will need to add the line
public static const STATE_SYSTEM_PAUSE:int = 99;
to the
com.efg.framework.FrameWorkStates.as
file if you have not already done so.
Adding the new constructor function
We will replace the current constructor function in the
Main.as
with this one.
public function Main() {
//added in chapter 11
if (stage) addedToStage();
else addEventListener(Event.ADDED_TO_STAGE, addedToStage,false,0,true);
}
The new constructor sets up a listener to wait until the stage is available. When it is available (or
if it is already available when the constructor is first run), the
addedToStage
function is called.
Adding the addedToStage function
This function will override the
GameFrameWork.as
version of the
addedToStage
function and then
call it with
super
:
override public function addedToStage(e:Event = null):void {
if (e != null) {
removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
}
super.addedToStage();
trace("in blastermines added to stage");
init();
}
The main purpose of this function is to add references to the stage object only after it is available
to the SWF file. We call the GameFrameWork class's addedToStage function with the
super.addedToStatge call and call the init function for our game.
Notice that we have moved the call to the
init
function to the end of the
addedToStage
function.
This ensures that we don't try to initialize the game until the
GameFrameWork
's
addedStage
function
has been called.
Next, we need to add in the
pausedScreen
. This instance of the
BasicScreen
framework class is
created in the
GameFrameWork.as
file, but it is instantiated in
Main
to allow color and size
customizations. You can also use
addChild
to add any custom elements that you feel you need to
this screen; it does not have to be just a plain screen with a background color, a button, and a
little text. This advice actually goes for all of the
BasicScreen
instances.
pausedScreen = new
BasicScreen(FrameWorkStates.STATE_SYSTEM_PAUSE,400,400,false,0xff000000 );
pausedScreen.createOkButton("UNPAUSE", new Point(150, 250), 100, 20, screenButtonFormat,
0x000000, 0xff0000,2);
pausedScreen.createDisplayText("Paused", 200, new Point(100, 150), screenTextFormat);