Greetings all,
I am making a FPS counter, Memory Usage and Time elapse tool for myself to see what my SWF’s are doing.
Anyone see where I can improve it? Any helpful advice is appreciated.
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.system.System;
import flash.utils.getTimer;
public class SWFHelper extends MovieClip {
private var mem:String;
private var lastCount:uint = getTimer();
private var ticker:uint = 0;
private var fps:Number;
private var timePlayed:uint = 0;
private var totTime:uint = 0;
private var currFrame:uint = 0;
private var totFrames:uint = 0;
public function SWFHelper() {
if (stage) {
init();
} else {
this.addEventListener(Event.ADDED_TO_STAGE, init);
}
}
private function init(e:Event = null):void {
this.removeEventListener(Event.ADDED_TO_STAGE, init);
totFrames = MovieClip(root).totalFrames;
totTime = totFrames / 24; //24 FPS
this.addEventListener(Event.ENTER_FRAME, update);
}
private function update(e:Event):void {
ticker++;
var nowCount:uint = getTimer();
var calcCount:uint = nowCount - lastCount;
if (calcCount >= 1000) {
fps = ticker / calcCount * 1000;
mem = Number(System.totalMemory/1024/1024).toFixed(2) + " MB";
ticker = 0;
lastCount = nowCount;
}
timePlayed = Math.floor(nowCount/1000);
currFrame = MovieClip(root).currentFrame;
if (currFrame == totFrames) {
this.removeEventListener(Event.ENTER_FRAME, update);
}
setElements();
}
private function setElements():void {
fPS.text = "FPS: " + fps.toFixed(1);
memory.text = "MEM: " + mem;
timeCount.text = "TIME PLAYED: " + timePlayed + " of " + totTime;
curFrame.text = "FRAMES PLAYED: " + currFrame + " / " + totFrames;
}
}
}