package  
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.utils.getTimer;
	
	/**
	 * ...
	 * @author beisik
	 */
	public class FPS extends Sprite 
	{
		private var text: TextField = new TextField();
		private var now: int, last: int, delta: uint;
		private var ticks: uint;
		
		public function FPS() 
		{
			ticks = 0;
			now = 0;
			last = 0;
			text.text = "FPS: 0.0";
			text.autoSize = TextFieldAutoSize.LEFT;
			addChild(text);
			x = 8;
			y = 8;
			width = text.width;
			height = text.height;
			text.backgroundColor = 0xFFFFFF;
			text.background = true;
			addEventListener(Event.ENTER_FRAME, onTick);
		}
		
		public function onTick(e: Event): void
		{
			ticks++;
			now = getTimer();
			delta = now - last;
			if (delta >= 1000)
			{
				text.text = "FPS: " + Number(ticks / delta * 1000).toFixed(1);
				ticks = 0;
				last = now;
			}
		}
		
	}

}