package nu.xero.flex { import flash.display.*; import flash.events.*; import flash.system.System; import flash.text.*; import flash.utils.getTimer; import mx.core.UIComponent; public class FlexFPS extends UIComponent { 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 FlexFPS(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.maxWidth, 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 = ""; label.textColor=textcolor; label.selectable=false; this.addChild(label); this.scaleX = 2; 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.toPrecision(8) + " bytes"; } else if(which=="kb"){ memory = (flash.system.System.totalMemory / 1000).toPrecision(8) + " kbs"; } else if (which=="mb"){ memory = (flash.system.System.totalMemory / 1000000).toPrecision(8) + " mbs"; } label.htmlText = " < framerate: "+ theFPS +" fps / "+ frameTime +"ms . memory: "+ memory +" . "+ other.toString() +" >"; theBar.scaleX = theBar.scaleX - ((theBar.scaleX - (frameTime/10)) / 5); } public function update(x:String):void { other = x; } } }