Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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);
}
share|improve this answer
    
That did it! Thankyou very much. I'm still not sure what was wrong with what I had before. –  user1133079 Jun 1 '12 at 21:37
    
@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. –  bummzack Jun 2 '12 at 9:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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