Do you like my tutorials?

Then consider supporting me on Ko-fi

Talking about Actionscript 3 and Flash.

Do you want to know how your latest Flash project is performing?

Here is movieMonitor, a little widget useful to display FPS, memory usage, stage size and number of children.

It’s based upon Hi-ReS-Stats by Mr.doob, but I added some new features to fit my needs, using some of the new System properties introduced by Flash Player 10.1

The usage is simple: just add

stage.addChild(new movieMonitor());

And you’re done. You can change stage with any DisplayObject if you want.

Look at this example:

Let’s see what do these figures mean:

FPS: current amount of frames per second (after the “/” you can see the amount of frames per second set in movie properties)

Minute average: the average amount of frames per second in the latest minute

ms per frame: the amount of milliseconds needed to draw the current frame

Direct: the amount of memory (in Megabytes) currently in use that has been directly allocated by Flash Player

Max direct: the maximum amount of memory allocated by the Flash Player so far

Total: the entire amount of memory (in Megabytes) used by the application, including the memory used by the container application, such as the web browser

Garbage: the amount of memory (Megabytes) that is allocated but not in use. This unused portion of allocated memory fluctuates as garbage collection takes place

Width: the width of the stage, in pixels. The width is calculated based on the bounds of the content of the stage (after the “/” you can see the stage width set in movie properties)

Height: the height of the stage, in pixels. The height is calculated based on the bounds of the content of the stage (after the “/” you can see the stage height set in movie properties)

Children: the number of DisplayObjects in the movie.

This is the script:

package {
	import flash.display.Sprite;
	import flash.display.DisplayObjectContainer;
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.system.System;
	import flash.text.StyleSheet;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.utils.getTimer;
	public class movieMonitor extends Sprite {
		private var xml:XML;
		private var theText:TextField;
		private var fps:int=0;
		private var ms:uint;
		private var lastTimeCheck:uint;
		private var maxMemory:Number=0;
		private var fpsVector:Vector.=new Vector.();
		private var childrenCount:int;
		public function movieMonitor():void {
			xml =
			
			FPS MONITOR
			FPS: 
			-
			Minute average: 
			-
			ms per frame: 
			-
			MEMORY MONITOR
			Direct: 
			-
			Max direct: 
			-
			Total: 
			-
			Garbage: 
			-
			STAGE MONITOR
			Width: 
			-
			Height: 
			-
			Children: 
			-
			;
			var style:StyleSheet = new StyleSheet();
			style.setStyle("xml",{fontSize:"9px",fontFamily:"arial"});
			style.setStyle("sectionTitle",{color:"#FFAA00"});
			style.setStyle("sectionLabel",{color:"#CCCCCC",display:"inline"});
			style.setStyle("framesPerSecond",{color:"#FFFFFF"});
			style.setStyle("msFrame",{color:"#FFFFFF"});
			style.setStyle("averageFPS",{color:"#FFFFFF"});
			style.setStyle("directMemory",{color:"#FFFFFF"});
			style.setStyle("veryTotalMemory",{color:"#FFFFFF"});
			style.setStyle("garbageMemory",{color:"#FFFFFF"});
			style.setStyle("directMemoryMax",{color:"#FFFFFF"});
			style.setStyle("widthPx",{color:"#FFFFFF"});
			style.setStyle("heightPx",{color:"#FFFFFF"});
			style.setStyle("nChildren",{color:"#FFFFFF"});
			theText = new TextField();
			theText.alpha=0.8;
			theText.autoSize=TextFieldAutoSize.LEFT;
			theText.styleSheet=style;
			theText.condenseWhite=true;
			theText.selectable=false;
			theText.mouseEnabled=false;
			theText.background=true;
			theText.backgroundColor=0x000000;
			addChild(theText);
			addEventListener(Event.ENTER_FRAME, update);
		}
		private function update(e:Event):void {
			var timer:int=getTimer();
			if (timer-1000>lastTimeCheck) {
				var vectorLength:int=fpsVector.push(fps);
				if (vectorLength>60) {
					fpsVector.shift();
				}
				var vectorAverage:Number=0;
				for (var i:Number = 0; i < fpsVector.length; i++) {
					vectorAverage+=fpsVector[i];
				}
				vectorAverage=vectorAverage/fpsVector.length;
				xml.averageFPS=Math.round(vectorAverage);
				var directMemory:Number=System.totalMemory;
				maxMemory=Math.max(directMemory,maxMemory);
				xml.directMemory=(directMemory/1048576).toFixed(3);
				xml.directMemoryMax=(maxMemory/1048576).toFixed(3);
				xml.veryTotalMemory = (System.privateMemory/1048576).toFixed(3);
				xml.garbageMemory = (System.freeMemory/1048576).toFixed(3);
				xml.framesPerSecond=fps+" / "+stage.frameRate;
				xml.widthPx=stage.width+" / "+stage.stageWidth;
				xml.heightPx=stage.height+" / "+stage.stageHeight;
				childrenCount=0;
				countDisplayList(stage);
				xml.nChildren=childrenCount;
				fps=0;
				lastTimeCheck=timer;
			}
			fps++;
			xml.msFrame=(timer-ms);
			ms=timer;
			theText.htmlText=xml;
		}
		function countDisplayList(container:DisplayObjectContainer):void {
			childrenCount+=container.numChildren;
			for (var i:uint=0; i < container.numChildren; i++) {
				if (container.getChildAt(i) is DisplayObjectContainer) {
					countDisplayList(DisplayObjectContainer(container.getChildAt(i)));
				}
			}
		}
	}
}

And this is the class for you to download. If you'd like to see more features, just let me know and I will be glad to add them.

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.