src/nu/xero/flash/FlashFPS.as
package nu.xero.flash {
import flash.display.*;
import flash.text.*;
import flash.utils.getTimer;
import flash.events.*;
import flash.system.System;
public class FlashFPS extends Sprite {
private var theBar :Sprite = new Sprite();
private var theBG :Sprite = new Sprite();
private var label :TextField = new TextField();
private var time :Number;
private var frameTime :Number;
private var prevFrameTime :Number = getTimer();
private var secondTime :Number;
private var prevSecondTime :Number = getTimer();
private var frames :Number = 0;
private var theFPS :String = "...";
private var other :String = "";
private var memory :String = "";
private var which :String = "";
public function FlashFPS(barcolor:uint = 0x006600, bgcolor:uint = 0x333333, textcolor:uint = 0xFFFFFF, bgalpha:Number = 1, memorydisplay:String = "kb"){
theBG.graphics.beginFill(bgcolor, 1);
theBG.graphics.drawRect(0, 0, this.width, 10);
theBG.graphics.endFill();
theBG.alpha = bgalpha;
this.addChild(theBG);
theBar.graphics.beginFill(barcolor, 1);
theBar.graphics.drawRect(0, 0, 25, 10);
theBar.graphics.endFill();
this.addChild(theBar);
label.autoSize=TextFieldAutoSize.LEFT;
label.htmlText = "<font face=\"verdana\" size=\"5\"></font>";
label.textColor=textcolor;
label.selectable=false;
this.addChild(label);
this.scaleX = this.scaleY = 2;
which = memorydisplay;
this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
public function onEnterFrame(e:Event):void {
time = getTimer();
frameTime = time - prevFrameTime;
secondTime = time - prevSecondTime;
if(secondTime >= 1000){
theFPS = frames.toString();
frames = 0;
prevSecondTime = time;
} else {
frames++;
}
prevFrameTime = time;
if(which=="b"){
memory = flash.system.System.totalMemory.toFixed(0) + " bytes";
} else if(which=="kb"){
memory = (flash.system.System.totalMemory / 1000).toFixed(0) + " kbs";
} else if (which=="mb"){
memory = (flash.system.System.totalMemory / 1000000).toFixed(0) + " mbs";
}
label.htmlText = "<font face=\"verdana\" size=\"5\"> < framerate: "+ theFPS +" fps / "+ frameTime +"ms . memory: "+ memory +" . "+ other.toString() +" ></font>";
theBar.scaleX = theBar.scaleX - ((theBar.scaleX - (frameTime/10)) / 5);
}
public function update(x:String):void {
other = x;
}
}
}