jump to navigation

Building a Frame Rate Counter in ActionScript 3.0 September 9, 2010

Posted by Tournas Dimitrios in Actionscript.
trackback

Frame Rate, or Frames per Second (fps), is the frequency at which an imaging device produces unique consecutive images called frames. The term applies equally well to computer graphics, video cameras, film cameras, and motion capture systems.

Each frame is a still image; displaying frames in quick succession creates the illusion of motion. The more frames per second (fps), the smoother the motion appears.

Measuring the fps will enable you to check the performance of your movie or application and make optimizations if necessary.

This can be very useful when testing an application that will run in different environments, the internet browser, the desktop, television boradcast or a mobile device.

With a little help from ActionScript 3, we’ll calculate the frame rate using the getTimer() method and create a TextField to display the results, realtime, in the Stage.

var startTime:Number;
var framesNumber:Number = 0;
var fps:TextField = new TextField();

function fpsCounter():void
{
	startTime = getTimer();
	addChild(fps);

	addEventListener(Event.ENTER_FRAME, checkFPS);
}

function checkFPS(e:Event):void
{
	var currentTime:Number = (getTimer() - startTime) / 1000;

	framesNumber++;

	if (currentTime > 1)
	{
		fps.text = "FPS: " + (Math.floor((framesNumber/currentTime)*10.0)/10.0);
		startTime = getTimer();
		framesNumber = 0;
	}
}

fpsCounter();

Document Class Version :

Perhaps you’d rather use a document class than timeline code?

package
{
	import flash.display.MovieClip;
	import flash.text.TextField;
	import flash.events.Event;
	import flash.utils.getTimer;

	public class FPSDemo extends MovieClip
	{
		public var startTime:Number;
		public var framesNumber:Number = 0;
		public var fps:TextField = new TextField();

		public function FPSDemo()
		{
			fpsCounter();
		}

		public function fpsCounter():void
		{
			startTime = getTimer();
			addChild(fps);

			addEventListener(Event.ENTER_FRAME, checkFPS);
		}

		public function checkFPS(e:Event):void
		{
			var currentTime:Number = (getTimer() - startTime) / 1000;

			framesNumber++;

			if (currentTime > 1)
			{
				fps.text = "FPS: " + (Math.floor((framesNumber/currentTime)*10.0)/10.0);
				startTime = getTimer();
				framesNumber = 0;
			}
		}
	}
}

Just copy it all into a new AS file and save it as “FPSDemo.as”, then you can use it whenever you like. You could use this class to embed a FPS counter inside any project like so:

var fpsDemo:FPSDemo = new FPSDemo();
addChild( fpsDemo );

.

Advertisements

Comments»

No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s