Jump to content
GreenSock

Search In
  • More options...
Find results that contain...
Find results in...
Rolf

DelayedCall or Timer class

Recommended Posts

Hi 

I'm working on a small game (as3) and I'm trying to optimize performance.

Instead of calling a method for hitTest every frame I want to call them 5 times per second.

 

I'm considering using delayedCall (because I'm using TweenMax.pauseAll): 

 

Something like this; 

 

updateCaller = TweenLite.delayedCall(0.2, handleHitTest);

 

private function handleHitTest():void {

     // handle HitTest here

   

    updateCaller.restart(true);

}

 

 

Would this be the best approach or would it be better to use the build in Timer class. 

 

Thanks

Rolf 

 

 

 

 

Link to comment
Share on other sites

A setup like that (with the hittest restarting/calling the next hittest in 200ms) would allow for quite a bit of time drift. Ideally the 1000th hitTest should occur after 200s (or as close as Actionscript can get it to be) from now, but with drift that could occur at 201s, or 202s, or even further...

 

Let's assume the timers fire perfectly for this example, and say your hittest takes exactly 10ms to run every time; this means that the following hittest will occur 210ms from the previous, not 200ms as you would hope. After 1000 hittests, you'll have drifted 10000ms so your 1000th hittest will be occurring at 210s.

 

To be honest a good old Timer with interval delay (or just setInterval) would be fine for something like this, but if you want to use GSAP syntax a repeating timeline should prevent that drift over time:

 

var interval = new TimelineMax({ repeat: -1 }).add(handleHitTest, 0.2);
  • Like 1
Link to comment
Share on other sites

Perfect!. And thank you for explaining the "time drift".  8-)

Link to comment
Share on other sites

For the record, I would recommend using GSAP for this rather than a Timer or setInterval() so that your logic runs in a synchronized fashion with the animations. In fact, you might want to add a listener to the TweenLite.ticker and add some conditional code inside the handler so that it only runs ever X number of ticks (or X amount of time) because that way, you know for sure that it'll run immediately AFTER all of the tweens rendered on that frame. 

 

If you use a Timer or setInterval(), those could fire arbitrarily between GSAP ticks (not ideal, although it'd probably be fine). I just like having all my hit testing run after my tweens have all rendered so that things are in their new positions. 

 

Minor detail. And again, you'd probably be fine with Timer or setInterval() in most situations, but I'm a big fan of the synchronization thing. 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×