Game Development Reference
In-Depth Information
GETTIMER
This method has been around since Flash 4 and still proves its usefulness
to this day. It returns the number of milliseconds that have passed since
the Flash Player started running. It is perfect for calculating time spent
between frames or any other pair of events. It should be noted that you
cannot rely on the method to return a specific number or always start
from 0. If multiple instances of the Flash Player are open, they all share
the same value, and whichever one opened first started at 0.
We
'
ll look at this class in a single pass, since it is relatively
short.
package drivingsim {
import flash.display.Sprite;
import flash.events.Event;
import flash.utils.getTimer;
public class Time extends Sprite {
static private var _instance:Time = new Time();
static private var _currentTime:int;
static private var _previousTime:int;
public function Time() {
if (_instance) throw new Error( " The Time class cannot
be instantiated. " );
addEventListener(Event.ENTER_FRAME, updateTime,
false, 0, true);
_currentTime = getTimer();
}
private function updateTime(e:Event):void {
_previousTime = _currentTime;
_currentTime = getTimer();
}
static public function get deltaTime():Number {
return (_currentTime - _previousTime) / 1000;
}
}
}
This class instantiates a single instance of itself in memory and pre-
vents any other instantiations. The one static, public method it has is a
getter for deltaTime . Every frame cycle, the class updates the current
and previous times so at any moment it is ready to return an accurate
delta. Since I like to work in seconds rather than in milliseconds,
Search Nedrilad ::




Custom Search