Game Development Reference
In-Depth Information
Here is where the real processing takes place. The timeDifference is passed into the update
function, and then we calculate how this value will affect the step of each game character's
movement. This step value is calculated using a variable called timeBasedUpdateModifier , which
we will create in the Game.as base class. This variable is the number of slices we want the game to
update per second. This is the same as the game's frame rate. This value will be assigned by the
Main class based on the stage frame rate.
We calculate the local step variable by dividing the timeDifference by 1,000 (the number of
milliseconds in a second) and multiplying this by the timeBasedUpdateModifier . This will be the
step modifier for the movement of each game character.
For example, if the timeDifference is 20 , meaning that it took 20 milliseconds to run the last
frame, and we have an example frameRate of 40 ( timeBasedUpdateModifier = 40 ), let's see what
the step would be:
timeDifference= 20
timeDifference/1000=0.02
timeBasedUpdateModifier=40
step=0.8
The step is the percentage of the distance we want each of our game characters to move this
frame tick. The calculated movement distance for each game character will be multiplied by this
value. Here is an example of this calculation for the Mine enemy characters from Blaster Mines; it
is in a class called Mine that we will create for the game:
public function update(step:Number=1):void {
//trace("updateModifier=" + updateModifier);
nextX+=dx*speed*step;
nextY+=dy*speed*step;
As you can see in the code, the nextX and nextY values are calculated and then multiplied by the
step value. This calculation allows us to modify the percentage of the distance moved based on
the current frame rate. The complete code for all of these functions will be presented as we
create the final Blaster Mines game.
Optimizing using render profiling
The FrameRateProfiler class we will create is used to profile the user's system to check if it can
play the game at the desired frame rate.
Let's create this class as the first new file for the Blaster Mines game. It will be part of the game
framework package structure.
Here is the class file name and location:
/source/classes/com/efg/framework/FrameRateProfiler.as
Here is the complete code for the FrameRateProfiler class. We will discuss it in detail after you
have taken a look at all of the code:
package com.efg.framework
{
import flash.display.*;
import flash.events.*;
import flash.utils.Timer;
Search WWH ::




Custom Search