views:

1179

answers:

4

I have an EnterFrame event, and I want to know the exact time between calls, so I will be able to animate objects more smoothly when the computer can't produce the desired framerate.

+2  A: 

use getTimer(); ?

+6  A: 

To be a little more specific.

currentTime = getTimer();
diff = currentTime - prevTime;

prevTime = currentTime;//update for next go around

EDIT

getTimer requires you import the package: flash.utils.getTimer;

Tyler Egeto
+1  A: 

You should use the inbuilt Timer function to call a method at a somehow* ENTER_FRAME independent way.

var timer:Timer = new Timer(500, 0);
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.start();

private function timerHandler(event:TimerEvent):void
{
    // do something

    // *EDIT* Thanks @Luke spotting this out (check comments)
    event.updateAfterEvent();

}

*) Still you need to keep in mind the event handler will not be triggered between frames so in case your script is lagging (because of some other process) this call will also be delayed. To be precise, the method call will be approximated at the frame executed at the same time or right after the set delay time.

Theo.T
The one problem, or rather reason you don't need to do this, is that the render will not update until the next enter frame anyways, so there is no reason not to wait for it and update you properties then. Also, by useing the getTimer method you are using the internal timer that always exists, rather than creating a new one, which is cleaner and adds less "weight" to your application. (Not that a Timer and event will add a lot of weight)
Tyler Egeto
OK. That is not entirely true. If you check the documentation you see that TimerEvent has a method called updateAfterEvent. This method let's you update the screen independent from the set frame rate.
Luke
Correct! Forgot about that one!
Tyler Egeto
Cheers Luke. I added the code in the example. Quite curious about this, would be worth to some benchmark tests in different scenarios (heavy display list, background process etc.) in order to verify the reliability. @kalvi, would be interesting to let us know how this works out in your case. Thanks,
Theo.T
+1  A: 

as the others stated, getTimer is the best way to go ... but i wanted to suggest something else: if you want to do time based animation, that are updated frame based, you might also try some of the bigger ActionScript tweening libraries, as the caurina Tweener ... they simply do that out of the box and provide other great features ...

back2dos