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;...and you're done. The Class offers two public methods -- start() and stop() -- but it starts by default.
myFPS = new FramesPerSecond();
addChild(myFPS);
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:
Post a Comment