2
\$\begingroup\$

Here is how I've been trying to implement delta time based on different internet resources.

var startTime:Number = getTimer();
game.Update(deltaTime);
deltaTime = Number(getTimer() - startTime) * 0.001;

My issue with this is it doesn't seem to be giving me accurate timing. The main update shows the frame time at 0.001 and when reinitializing the level it goes to 0.002. I'm using dt else where for a timer and later on time based physics so I would like it to work as expected. I must be missing something silly.

\$\endgroup\$
3
\$\begingroup\$

I have tried different methods to get the elapsed time and this one has worked best for me so far:

private var _t:int;

// initialize _t and add ENTER_FRAME listener
private function init():void {
    _t = getTimer();
    addEventListener(Event.ENTER_FRAME, onEnterFrame);
}

private function onEnterFrame(e:Event):void {
    var t:int = getTimer();
    var dt:Number = (t - _t) * 0.001;
    _t = t;
    game.update(dt);
}
|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ That did it! Thankyou very much. I'm still not sure what was wrong with what I had before. \$\endgroup\$ – Kara Jun 1 '12 at 21:37
  • \$\begingroup\$ @user1133079 Glad it helped. The problem with your approach was that you were only measuring the time needed for your update call. You need to measure the time used for a frame and therefore only use getTimer once in the update. If your update takes less time than the current FPS, your delta time will be 1/FPS if your update takes longer (due to heavier calculations) the next frame will be delayed and it will also be correctly reflected in the dt variable. \$\endgroup\$ – bummzack Jun 2 '12 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.