Intervals instead of onEnterFrame?

14 posts

Flag Post

I would like to learn a way to keep game elements moving at the same rate regardless of the rendered frame rate.

For example, if your computer is slow, many flash games simply go slower. However, in a time based game or a multiplayer environment, a game would need to keep track of time and somehow position objects at the right space with a slow frame rate or a fast frame rate.

Does anyone know how to accomplish this in Flash, with intervals or similar?

Also, is there a way to get the overall played time of any given movieclip or SWF file? In other words being able to access as a variable the exact point in time during the playing of a movie.

Flag Post

This is kinda simple instead of onenterframe us setInterval example:

as 2.0

var myInterval = setInterval(traceHello,10);

//traceHello will get executed every 10 ms

function traceHello(){

trace(“Hello World”);


//to stop the interval..



in AS 3:(way better :P)

as 3.0


var myTimer:Timer(10,0)

//traceHello will get executed every 10 

//ms(0 is the repeat count 0 = endless)


//add an event 

//instead of .TIMER you also have TIMER_COMPLETE


function traceHello(evt:TimerEvent){

	trace("Hello World");


//to stop the interval..


hope this helps

and sorry for the crappy formatting….

Flag Post

Just remember that intervals and EnterFrames should be juggled. Intervals dont run as smoothly as EnterFrame events, even when they are set to the same speed, so dont imagine them as the cure all or as mandatory for time movement. You should always test both, and remember that you can also do time based movement using EnterFrame events by calculating how much time has passed since the last event, then using that as a factor in your next calculations.

(boluc, I edited your post to fix the formatting)

Flag Post

The as 2 code wouldn’t work because you set the interval before the computer knows the function, it’d give you an error.

Flag Post

Actually Woad the compiler parses the functions before it tries to evaluate the code. It’s just the way compilers work in any language that I’ve used. So, boluc’s code would work fine. In fact, if you had tested it (like I just did) you would see that there is no error :/

Flag Post

by calculating how much time has passed since the last event

how do you do that exactly?

Flag Post

Use the date object or the getTimer() method. Mark the time: lastTime = getTimer(), then the next time you calculate the difference: getTimer()-lastTime.

Flag Post

Ah excellent thanks! I was looking for a function like this for a long time. Thanks for helping this AS noob out :)

BTW how do people write in AS 3.0 already?

Flag Post

No problem.

People write AS 3.0 either usually with the Flex SDK or else the Flash CS3 beta if they are a beta tester (rarely).

UPDATE: Flash CS3 is now available for purchase!

Flag Post

I use intervals (in AS2) and automatically adjust the delay depending on how long it actual took each frame. I’ll post the code later.

You could also try making a silent sound file of the length you want, and then attaching the loop code to its onSoundComplete event. It’s more consistent than setInterval at long delays, but I haven’t tried it for intervals of less than a second. Also, all sounds automatically stop if you jump to a previous frame of the main timeline.

Flag Post


i write in as 3 with the flash 9 public alpha wich is just removed from adobe labs since flash cs3

(flash 9 isnt flash cs3 at all its just flash 8 with AS 3 support)


if you make the interval the same as the current fps why not use onenterframe i think setIntervals are handy

because they dont slow down where there is a lot to be drawn

used to c(++) ?

Flag Post

i write in as 3 with the flash 9 public alpha wich is just removed from adobe labs since flash cs3

Yes, I used the alpha intially and it was horrible. As you alluded to its basically a repackaged version of Flash 8 to compile some AS 3 code and far far different from the actual CS3 environment. Most people seemed to prefer to use Flex.

Flag Post

boluc, I can’t understand what you’re trying to say, sorry.

Here’s the code I use to keep the frame rate fairly constant:

var main = function() {

/* main loop code goes here */

// adjust interval to maintain constant frame rate
var t = getTimer();
var dt = t – time;
time = t;

if (dt > 35) {
if (interval > 1) {
// speed up
interval -= 2;
id = setInterval(main, interval);
} else if (dt < 30) {
// slow down
interval += 3;
id = setInterval(main, interval);

var time;
var interval;
var id;

To start running the main loop, use this code:

// start the main loop
time = 0;
interval = 30;
id = setInterval(main, interval);

Let me know if you have any questions.

Flag Post

Great example thank you!