10May/0614
setInterval or onEnterFrame… your thoughts?
This is by no means a new subject of discussion, but I’ve been thinking lately about how some people use setInterval more and some people use onEnterFrame more. I personally like to use setInterval mainly because I have control over the speed at which the function is called as opposed to having to rely on the frame rate. I know that there are instances where one or the other might be specifically called for, but which do you prefer to use more often and why?















May 10th, 2006 - 16:28
Well, I was always a fan of onEnterFrame until I started working in the advertising world. Basically, because of changes in artisitc direction, we were constantly having to change the speed at which things happend. setInterval allows you do this quickly.
May 10th, 2006 - 16:29
Arguably, both methods can give the same results if you’re calculating the time on each interval or frame and doing some math based on how much time has passed. That’s not always the case, but it’s possible.
Personally, I prefer setInterval (and the Timer class in AS3). I like it better because it doesn’t have to rely on the framerate. You might not necessarily see every update, but if you decide to alter the framerate later, you won’t have to make as many changes.
May 10th, 2006 - 16:37
it depends on what you’re doing. setInterval can save a lot of player cycles.
May 10th, 2006 - 16:39
I prefer setInterval in most cases, having code execute on every frame using onEnterFrame is overkill for a lot of situations. Think about it, having your function called 12 or more times a second is pretty intensive.
For a lot of purposes I just have an interval running every 500 ms — for everything other than tweening that works a charm.
The Timer class in AS3 is awesome though, wonder if someone made an implementation for AS2 yet.
May 10th, 2006 - 16:40
If I need to know when each frame happens, I’ll use onEnterFrame since it’s the only function that will fire every frame consistently. setInterval can, but sometimes more, sometimes less; no guarentees whereas with onEnterFrame, you are guarenteed you’re code will run. This is useful when tracking the status of animations, and is in fact the only way for frame animated MovieClips.
For everything else, setInterval.
May 10th, 2006 - 16:47
Also, even though it’s deprecated in AS3, setTimeout is still pimp for one-shot timers.
May 10th, 2006 - 17:31
As I said to Sam the other day, “you’ll get my enterframe when you pry it from my cold dead hands.” But seriously, I use enterframe mostly out of habit. And in AS2, it was a lot quicker to set up, just make a function called onEnterFrame and it runs. In AS3, you have to set up the listener and import the Event class anyway, so my last excuse is gone and I’ll probably start using intervals more often.
May 10th, 2006 - 17:36
onEnterFrame mostly. Most of what I use it for is motion. Motion and framerate go hand in hand. Framerate and onEnterFrame go hand in hand. onEnterFrame and motion go hand in hand.
May 10th, 2006 - 17:44
I’ve fallen in LOVE with setTimeout…
May 10th, 2006 - 17:45
Like Jesse mentioned, setTimeout is great. If you’d prefer to use a Timer in AS3, the constructor has a parameter that defines how many times the Timer runs. That’s pretty nice.
May 10th, 2006 - 19:19
package {
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.display.Sprite;
public class TimerExample extends Sprite {
public function TimerExample() {
var myTimer:Timer = new Timer(1000, 2);
myTimer.addEventListener(“timer”, timerHandler);
myTimer.start();
}
public function timerHandler(event:TimerEvent):void {
trace(“timerHandler: ” + event);
}
}
}
May 11th, 2006 - 17:14
Intervals in flash are also inaccurate and are influenced from outside the flash player. So even if you use intervals you’ll never get perfect control.
http://blog.guya.net/2006/05/08/flash-player-inaccuracy-clarified/
http://blog.guya.net/2006/03/14/check-your-interval/
August 23rd, 2006 - 07:04
What about using updateAfterEvent(), this has been added to the timer event so you can cause your display to redraw after the timer tick.
e.g.
private function timerHandler(e:TimerEvent):void{
draw();
e.updateAfterEvent();
}
So apparently you can have your swf running at 1fps but your timer will cause it to draw.
Has anybody tried this? I am currently playing with and the other methods mentioned with my 3d engine(http://johnukmn.blogspot.com/2006/08/introducing-lux.html)
September 7th, 2006 - 23:16
Like you I also also like to use setInterval to control over the speed at which the function is called.