Wednesday, April 2, 2008

Determining the running frame rate in an Flash AS3 movie

I'm working on a DVD project that has over 200 photos in the main movie and have been having a LOT of trouble with the framerate. But I wasn't able to see what the actual running framerate was and what was causing my issues. There are lots of results in Google on "framerate in AS3" but all about how you can now dynamically change the framerate of an AS3 movie at runtime -- not how to determine the actual current framerate of a running movie.

Anyway, I did find one that was written in AS2 and with MANY thanks to TechMono, I've modified it for use in AS3. So, to pass on the favor, here it is. To set it up, create a new instance of the class and use addChild() to place it at the top level of your movie:
private var myFPS:FramesPerSecond;

myFPS = new FramesPerSecond();
addChild(myFPS);

...and you're done. The Class offers two public methods -- start() and stop() -- but it starts by default.

Here is the Class:

package {
import flash.display.Sprite;
import flash.text.*;
import flash.utils.*;
import flash.events.*;

public class FramesPerSecond extends Sprite {
private var fps_txt:TextField = new TextField();
private var m_counter:int = 1;
private var m_startTime:Number;
private var m_updateRate:int;
private var m_precision:int;
private var txtFormat:TextFormat;

public function FramesPerSecond(updateRate:Number=6,precision:Number=1) {
m_startTime = getTimer();
m_updateRate = updateRate;
m_precision = Math.pow(10, precision);
fps_txt.width =100;
fps_txt.autoSize = TextFieldAutoSize.LEFT;
fps_txt.textColor=0xFFFFFF;
fps_txt.opaqueBackground = true;
fps_txt.backgroundColor=0x000000;
txtFormat = new TextFormat("Verdana",13);
fps_txt.setTextFormat(txtFormat);
addChild(fps_txt);
start();
}

private function update(evt:Event):void {
if (m_counter == m_updateRate) {
var stopTime:Number = getTimer();
var elapsedTimeInSecs:Number = (stopTime - m_startTime) / 1000;
var fps:Number = m_updateRate / elapsedTimeInSecs;
fps = Math.floor(fps * m_precision) / m_precision;
fps_txt.text = "FPS: " + fps;
fps_txt.setTextFormat(txtFormat);
this.m_startTime = stopTime;
this.m_counter = 1;
} else {
this.m_counter++;
}
}

public function start():void{
this.addEventListener(Event.ENTER_FRAME,update);
}

public function stop():void{
this.removeEventListener(Event.ENTER_FRAME,update);
}
}
}


...if you want a good explanation of how it works, best to go back to the original creator. As for me, I am VERY happy to have this and was able to identify the frame-rate-killing function immediately ;-)

No comments: