Posts Tagged ‘com.efnx’

AS3 – Calculating true Frames Per Second

Monday, October 22nd, 2007

I strongly recommend you look at this new version of this class.

When coding for Flash based games it’s important to know how fast your project is running [even if you're not coding games it's good to know whether or not your code is slogging down the player]. Adobe’s Stage class provides a parameter called “frameRate,” which allows you to dynamically get and set the player’s frame rate. Well, not exactly. It allows you to get and set the player’s TARGET frame rate, the fact is if the player can’t perform your code at that frame rate, it won’t! So how can you find out what the real frame rate is?

I wrote a simple class for this task to use in my project development. It’s super simple. My class, fpsBox, adds an eventListener to the stage [if you pass the stage as an argument when instantiating the class] or to itself, that triggers every frame and increments a value. A timer is started simultaneously with a repeat cycle of one second. Every second the number of counts counted each frame are displayed in the fpsBox and then averaged in an array of instantaneous values. Don’t worry, the array never gets past two values, the current average and the instantaneous FPS. Here’s some usage:

import com.efnx.fps.fpsBox;

var fps:fpsBox = new fpsBox(stage);

That’s it!

What it looks like:

Link to SWF

Source Code
fpsBox Class Source Code

Follow me on GitHub
Follow me on Google+
Follow me on Twitter