AS3 FPS and Memory Counter Package
One of my favorite class I used to include when developing games for flash is my FPS and Memory Counter Package.
I use this to check my fps and memory consumption for my games.
This helps to identify which of my methods I wrote is taxing the CPU or not.
Simply import package to your main.as and add it into the stage.
Refer to the sample codes below to for more details.
// To use the class simply create a instance of it and add it to the stage. // var MyFPSMemCounter : FPSMemCounter = new FPSMemCounter (); // addChild( MyFPSMemCounter ); package { import flash.display.Stage; import flash.system.System; import flash.events. *; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.utils.getTimer; public class FPSMemCounter extends TextField { private var fontSize : Number; //the font size for the field private var lastUpdate : Number; // the results of getTimer() from the last update private var frameCount : Number; //stores the count of frames passed this second private var currentTime : Number; private static const UPDATE_INTERVAL : Number = 1000; //the interval at which the frame count will be be posted public function FPSMemCounter (textColor : Number = 0xFFFFFF, fontSize : Number = 25) : void { this.textColor = textColor; this.fontSize = 12; //set the field to autosize from the left autoSize = TextFieldAutoSize.LEFT; //make the text unselecteable and disable mouse events selectable = false; mouseEnabled = false; addEventListener (Event.ADDED_TO_STAGE, setFPSUpdate); addEventListener (Event.REMOVED_FROM_STAGE, clearFPSUpdate); } //called when the instance is added to a Display Object private function setFPSUpdate (event : Event) : void { addEventListener (Event.ENTER_FRAME, updateFPS); frameCount = 0; updateText (frameCount); lastUpdate = getTimer (); } //called when the instance is removed from a Display Object private function clearFPSUpdate (event : Event) : void { removeEventListener (Event.ENTER_FRAME, updateFPS); } //update the frame counter private function updateFPS (event : Event) : void { //get the current time and increment the frame counter currentTime = getTimer (); frameCount ++; //post the frame count if more then a second has passed if (currentTime >= lastUpdate + UPDATE_INTERVAL) { lastUpdate = currentTime; updateText (frameCount); frameCount = 0; } } //update the display text private function updateText (frameNum : Number) : void { var mem:String = Number( System.totalMemory / 1024 / 1024 ).toFixed( 2 ) + 'Mb'; htmlText = "<span><strong>FPS : </strong>" + frameNum + " fps<strong> Memory : </strong>"+ mem +"</span>"; } } }





We should thank you for giving such a wonderful blog. Your site happens to be not only informative but also very imaginative too. We find a limited number of experts who can think to write technical articles that creatively. All of us are on the lookout for information on something like this. I Myself have gone through several blogs to build up on knowledge about this.We look forward to the next posts !!
There seems to be an error in the code. In this line:
if (currentTime >= lastUpdate + UPDATE_INTERVAL)
{
I assume by “>” you mean “>” ? I replaced it with “>=” and it works.
Another thing, you said:
// var MyFPSMemCounter : FPSMemCounter = new FPSMemCounter ();
// addChild( fpsCounter );
Shouldn’t it be
addChild( MyFPSMemCounter);
Instead? Those two things just confused me a bit.
Other than that, very useful class.
sorry bout the error, didnt notice when I was swapping those syntax plugin for my new wordpress theme. problem fixed.