Game Development Reference
In-Depth Information
The time-based step timer will keep the game objects running at the same speed across all
machines. We will also use the profiled rate and pass it into the
setRendering
function of the
Game
class. This will allow us to add or subtract game effects based on the profile of the user's machine.
The final three lines for the
FrameRateProfile
r add it to the
stage
display list, start it up, and add
a listener for when it is complete.
addChild(frameRateProfiler);
frameRateProfiler.startProfile(frameRate);
frameRateProfiler.addEventListener(FrameRateProfiler.EVENT_COMPLETE,
frameRateProfileComplete, false, 0, true);
When the profile is complete the
frameRateProfileComplete
function is called. Let's take a look at
this function now.
Creating the frameRateProfileComplete function
When the
FrameRateProfile
r has completed its work, the
frameRateProfileComplete
function is
called. Let's take a line-by-line look at how it does its work and what it accomplishes.
When the profile is complete, the first thing we do is call the new function called
setRendering
that we added to the
Game
class. We pass in the
profilerAverageFrameRate
and the actual
frameRate
we desire for the game. We can use these two values and set customized options for
game effects quality if needed. We will do this in the Blaster Mines game.
game.setRendering(frameRateProfiler.profilerFrameRateAverage, frameRate);
game.timeBasedUpdateModifier = frameRate;
In these two lines, we are simple removing the
FrameRateProfiler
from the screen and removing
the event listener:
removeEventListener(FrameRateProfiler.EVENT_COMPLETE, frameRateProfileComplete) ;
removeChild(frameRateProfiler);
These five lines add the optional
FrameCounter
instance to the stage:
//frame counter
frameCounter.x = 400;
frameCounter.y = 200;
frameCounter.profiledRate = frameRateProfiler.profilerFrameRateAverage;
frameCounter.showProfiledRate = true;
addChild(frameCounter);
Finally, we start the game timer. By passing in
true
, we will use the new time-based step timer
rather than the original game timer we created in Chapter 2:
startTimer(true);
Creating the Library.as class
The
Library.as
class file is only necessary for those using the Flex SDK framework. It is not
used for Flash IDE projects. The changes to the library concern adding the sounds as
static
const
assets rather than having them inside an exported SWF from the IDE. By doing this, we
free the game from needing the IDE at all. The one drawback to decoupling the game and IDE is
the inability of the Flex framework to import
.wav
files. By using
.mp3
files, we mitigate this