FPS AND MEMORY USED VIA AS3                                      as3


    This very short tutorial will show you how to create simple fps and memory usage display utility for your flash project. Useful to adjust for example textures quality automatically based on fps and memory of each user of your flash project. At the end it should look like this (the ball is there just to demonstrate the number change):



     Create new actionscript 3 flash file. Insert two dynamic text fields on the stage. Set their instance names to MEM_txt and fps_txt. Now open actions window and insert the following actiosncript code:


var frames:int;
var pastTime:Number;
var currentTime:Number;

function DisplayFPS() {
	frames=0;
	pastTime=0;
	currentTime=0;
	this.addEventListener(Event.ENTER_FRAME,CountFPS);
}

function CountFPS(e:Event):void {
	frames+=1;
	currentTime=getTimer();
	if (currentTime-pastTime>=1000) {
		fps_txt.text="FPS: "+ String(Math.round(frames*1000/(currentTime-pastTime)));
		pastTime=currentTime;
		frames=0;
	}
	MEM_txt.text="Memory used: "
					+String(Math.round(1000*System.totalMemory/1048576)/1000 +" MB");
}
DisplayFPS();


     First three lines are variables used in this script. Line 5 is the function that sets starting values for these variables, and is starting the fps and mem counting. Line 12 is function that counts the fps and mem used. Fps counting is based on time between executing this function on every frame refresh. Memory used is basicly only one command System.totalMemory. The definition of this command is totalMemory : uint [static] [read-only] The amount of memory (in bytes) currently in use that has been directly allocated by Flash Player or AIR.. You can read more about System properties here help.adobe.com. The very last lane starts the entire thing.


DOWNLOAD SOURCE FILE