Game Development Reference
In-Depth Information
Time-based step timer
: This will replace the
Timer
-based game timer with an
ENTER_FRAME
-based step timer. The goal of this type of timer is to keep the game
rendering and object movement constant when played at various frame rates.
Frame rate profiler
: This will test the player's machine capabilities before the game is
played. The result will be passed into the
Game
class and can be used to increase or
decrease game effects based on the profiled frame rate.
Frame counter
: The frame counter is used to calculate the current frame tick rate. It is
used by the frame rate profiler and can also be displayed during gameplay.
Note: The Entire GameFrameWork.as class code is re-printed at the end of this chapter. We
recommend that you use that version after reading through the changes.
Checking stage access
The first modification we will make to the
GameFrameWork.as
file is to add code that
Main
can call
when the stage is available. We do this because the
GameFrameWork.as
will need to add stage
listeners for the Pause and Mute keys. The stage must be available for this to happen properly.
Creating the New addedToStage function
Place this new function inside the
GameFrameWork.as
file:
public function addedToStage(e:Event = null):void {
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownListener);
this.focusRect=false;
stage.focus = stage;
trace("gamFrameWork added to stage");
}
The main purpose of this function is to add references to the stage object only after it is available to
the SWF file. We have done this to ensure that
Main
's
init()
function is not called before the stage
is available.
When we get to the “Adding pause and mute functionality to the framework” section of this chapter,
we will examine how this function is called. The
Main
class will override this to call its own version of
this function and then call this version with the
super.addToStage()
call.
Adding pause and mute functionality to the framework
Mute functionality will be added by placing a new set of key listeners in to the
GameFrameWork.as
class. These listeners will detect when the M key is pressed and then call a function in the
SoundManager
. These same listeners will be used for the pause functionality using the P key,
which will trigger the pause functionality.
The actual pausing of the gameplay will be handled by the
Game
class. In the
Main.as
class for a
game that implements pause functionality, we will override the
systemGamePlay
function and pass
the paused class variable to the
Game
class when we call the
runGame
function. This will actually
be a function called
runGameTimeBased
for the new step timer.