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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | // To use the class simply create a instance of it and add it to the stage. // var MyFPSMemCounter : FPSMemCounter = new FPSMemCounter (); // addChild( fpsCounter ); 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 = "<font size='" + fontSize + "'><b>FPS : </b>" + frameNum + " fps</b><b> Memory : </b>"+ mem +"</font>"; } } } |


























Virtual assistant india said
am February 8 2010 @ 4:48 pm
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 !!