movieMonitor: an AS3 performance monitor with 10.1 features

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.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

215 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stairs
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers