FPS related timer

7 posts

Flag Post

Is it possible to create a FPS related timer? for example if player gets 60 fps, – timer will be completed in 1 second, if he gets 30 fps, – timer will be completed in 2 seconds.

Maybe adding a secret movieclip and checking each 10th frame would work, but i’m looking for a more civilized way:/

Could anyone help with this?

 
Flag Post

Count frames.

Supposedly, you have an event listener somewhere near the top of your code’s hierarchy that listens for ENTER_FRAME events and calls some update function. Give that class a framesElapsed:uint variable that you increment in the update function. If you store the value of framesElapsed at some key moments you can then compute how many frames it’s been since then.

 
Flag Post

Hmm, that would work. Should I do it like this ?

(loop)

framesElapsed += 1;

if(framesElapsed >= 60)
{
     // call function
     framesElapsed = 0;
}
 
Flag Post

Yep, something like that.

 
Flag Post

Note that frame rates can vary from one machine to the next, and even on the same machine, there’s no guarantee that each frame will last the same number of milliseconds. If you need a more precise way of telling time, read this.

 
Flag Post

That would work as long as you’re only tracking one event. To track several, don’t reset the frame counter, but have each event keep a lastHappened:uint variable, and change your test to

framesElapsed++;

myEvent.happen(framesElapsed);

----

And in the proper class

public function happen(currentTime:uint):void
{
    if (currentTime < lastHappened + eventDelay) return; // eventDelay is a class constant that
        // defines the delay between events firing, in your case 60 frames.
    lastHappened = currentTime;
    fireEvent();
}

And Elyzius, the OP was specifically asking for a timer of variable duration depending on the user’s frame rate.

 
Flag Post
Originally posted by Ace_Blue:

That would work as long as you’re only tracking one event. To track several, don’t reset the frame counter, but have each event keep a lastHappened:uint variable, and change your test to

framesElapsed++;

myEvent.happen(framesElapsed);

----

And in the proper class

public function happen(currentTime:uint):void
{
    if (currentTime &lt; lastHappened + eventDelay) return; // eventDelay is a class constant that
        // defines the delay between events firing, in your case 60 frames.
    lastHappened = currentTime;
    fireEvent();
}

Thank you, currently i’m tracking only one event, but this is a very good suggestion for the future.