天天看點

FPS counter

該類在任何一個flash/flex程式都可以用,用于計算幀頻  和程式占用的記憶體 。      
package
{
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.utils.getTimer;
	import flash.system.System;
	public class FrameRater extends Sprite
	{
		public var frameRate_tf:TextField;
		public var memory_tf:TextField;
		public var lastTen:Array;
		public var lastTime:Number;
		public function FrameRater ()
		{
			frameRate_tf = new TextField();
			frameRate_tf.x = 0;
			frameRate_tf.y = 0;
			frameRate_tf.selectable = false;
			addChild (frameRate_tf);
 
			memory_tf = new TextField();
			memory_tf.x = 0;
			memory_tf.y = 15;
			memory_tf.selectable = false;
			addChild (memory_tf);
 
			lastTen = new Array();
			lastTime = getTimer();
		}
		public function Update ()
		{
			var timeTaken:Number = getTimer() - lastTime;
			lastTime = getTimer();
			lastTen.push (1000/timeTaken);
			var average:Number;
			var sum:Number = 0;
			for (var i in lastTen)
			{
				sum += lastTen[i];
			}
			average = sum/lastTen.length;
			if (lastTen.length > 10)
			{
				lastTen.splice (0,1);
			}
			frameRate_tf.text = ""+Math.round(average)+" FPS";
			memory_tf.text = ""+Math.round(System.totalMemory/1024/1024)+" MB";
		}
	}
}      
隻需在每幀調用Update :  下面是例子:      
package
{
	public class Project
	{
		public var fps:FrameRater;
		public function Project ()
		{
			fps = new FrameRater ();
			addChild (fps);
			addEventListener (Event.ENTER_FRAME,Update);
		}
		public function Update (e:Event)
		{
			fps.Update ();
		}
	}
}      

繼續閱讀