Game Development Reference
In-Depth Information
static private var mInstance:Time = new Time();
static private var mCurrentTime:int;
static private var mPreviousTime:int;
static private var mFixedDelta:Number = -1;
public function Time()
{
if (mInstance) throw new Error( " The Time class cannot
be instantiated. " );
addEventListener(Event.ENTER_FRAME, onUpdateTime,
false, 0, true);
mCurrentTime = getTimer();
}
private function onUpdateTime(_evt:Event):void
{
mPreviousTime = mCurrentTime;
mCurrentTime = getTimer();
}
static public function fixDelta(_value:Number = -1):void
{
mFixedDelta = _value;
}
static public function get deltaTime():Number
{
if (mFixedDelta > 0)
{
return mFixedDelta;
}
return (mCurrentTime - mPreviousTime) / 1000;
}
}
All of the values stored inside the class are static, but it creates an
internal instance of itself in order to create its own ENTER_FRAME
loop. As you can see, it stores values for the current time and the time
in the previous frame so that when the accessor for deltaTime is
called, it can compute it on the fly. There
'
s also an additional function
called fixDelta . In certain cases, like with a physics simulation, you
mightwantthedeltatimetoalwaysbeaconsistentvaluesoasto
maintain the fidelity of things like collision checking. This method
allows you to set the desired value that deltaTime will return every
time you call it. Although we won
t use it in this game, there are times
when it might well come in handy
'
there are even preset constants
defined for 24 and 30 fps deltas. To use this method, you
'
d simply call
Search Nedrilad ::




Custom Search