Game Development Reference
In-Depth Information
To ensure the games in the topic that were created before this chapter will still compile within the
framework we will only add pause functionality to the new
runGameTimeBased
function that we will
create for the time-based step timer rather than the
runGame
function we have used for all of the
previous games.
These are the public variables for the pause and mute functionality:
public static const KEY_MUTE:int = 77; // added chapter 11
public static const KEY_PAUSE:int = 80; //added chapter 11
public var paused:Boolean = false;
public var pausedScreen:BasicScreen;
Let's now look closely at the five new functions needed to handle pause and mute:
private function keyDownListener(e:KeyboardEvent):void
: This function will use a
switch:case
statement to determine if any action needs to be taken for the key that was
pressed. It will act on the M key for mute and the P key for pause by calling the handler
functions described next.
private function pausedKeyPressedHandler():void
: When the P key is pressed, this
handler function is called. It sets the new class Boolean variable,
paused
, to
true
. It also
puts the
pausedScreen
on display. The
pausedScreen
is a simple instance of the
BasicScreen
class, so we can listen for the click of the OK button on the screen. This is
the signal that the game player wants to close the
pausedScreen
. This function adds this
listener for the click on the OK button on the
pausedScreen
:
pausedScreen.addEventListener(CustomEventButtonId.BUTTON_ID , pausedScreenClickListener,
false, 0, true);
This function also adds the pausedScreen to the display list:
public function pausedScreenClickListener(e:Event):void
: This function will remove
the
pausedScreen
from the display list, remove the event listener for the OK button on the
pausedScreen
and set the
paused
class variable to
false
.
private function muteKeyPressedHandler():void
: When the M key is pressed, this
handler will call the
soundManager.muteSound
function Look for a description of this
function in the section on changes to the
SoundManager
class.
We also need to add this line to the com.efg.framework.FrameWorkStates.as file
with the rest of the constants:
public static const STATE_SYSTEM_PAUSE:int = 99;
For the full source to the
Main
class, see the section titled “The Main class.” For the full source to
the
GameFrameWork
class, see the section titled “The full
GameFrameWork
class code.”
Adding the time-based step timer
In Chapter 2, we introduced the basic timer we use for our game loop. This timer is implemented
with an instance of the
Timer
class that is set to run at an interval based on the
frameRate
variable. If we set the
frameRate
variable to
40
, the timer will run at a 1,000/40-millisecond
interval. The result was a decent timer that runs the current set of games in a nice manner. But, if