Game Development Reference
In-Depth Information
the games are played on a variety of machines and Flash Player versions, there will be
noticeable differences in frame rate. This will cause the game sprites to move faster or slower
than we intended.
When using this timer, we split the game tick into even slices of time for each game loop iteration
and hoped that the game would be able to process all code and render everything to the screen
inside each even time slice. So, when we moved the car in Drive She Said (for example) at its
speed of 4 pixels per tick, we simply moved the car 4 pixels every frame tick and hoped that in a
single second the car would travel 30
4 (our frame rate times the pixels-per-second speed
value). Since Drive She Said really was not a processor-intensive game, we were able to achieve
the 120 pixels per second movement rate that we desired. This is not always the case though. As
you will see with Blaster Mines, we are going to create a game with many moving objects that
some systems and platforms might not be able to handle properly. Because of this, we will add
some functionality to the time slice frame tick to ensure that all objects move the desired distance
in a single second. As a result, objects will not always move the exact same distance on each
frame tick, but in a single second, we will ensure that the objects move the distance we'd like. So,
even if the Flash Player is running at a slower frame rate than desired, we can ensure that our
game objects will move distance we want.
In this chapter, we are going to create a new type of timer that we call the time-based step
timer . This timer will implement the standard ENTER_FRAME event rather than the Timer class.
Instead of relying on interval time from the Timer call, we will use the stage frame rate to run our
game. We will not simply rely on this stage frame rate to keep our game running smoothly
though. We will be profiling the time it takes to run each frame tick and modifying the number of
pixels our game character move based on this information.
First, we will need to change the startTimer function to add in the ability to use the new timer:
public function startTimer(timeBasedAnimation:Boolean=false):void {
stage.frameRate = frameRate;
if (timeBasedAnimation) {
lastTime = getTimer();
addEventListener(Event.ENTER_FRAME, runGameEnterFrame);
}else{
timerPeriod = 1000 / frameRate;
gameTimer=new Timer(timerPeriod); //*** changed removed in new chapter 2
gameTimer.addEventListener(TimerEvent.TIMER, runGame);
gameTimer.start();
}
}
The startTimer function now accepts a Boolean parameter that allows us to switch between the
original timer and the new time-based step timer. By passing true, we switch to the
ENTER_FRAME version. Our game loop will now called the runGameEnterFrame function rather
than the original runGame function on each frame tick.
Adding the runEnterFrame function
The runEnterFrame function uses a getTimer call to time the number of milliseconds that have
passed after each frame tick has run. This is stored in the timeDifference class level variable.
public function runGameEnterFrame(e:Event):void {
timeDifference = getTimer() - lastTime
Search WWH ::




Custom Search