Advertisement
  1. Code
  2. ActionScript
Code

Quick Tip: How to Build a Frame Rate Counter in ActionScript 3.0

by
Difficulty:BeginnerLanguages:

In this Quick Tip you'll learn how to measure the current frame rate of your Flash application using ActionScript 3, and display the FPS on screen.


The Importance of Frame Rate

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.


Step 1: Create a New File

Open Flash and create a new Flash File (ActionScript 3).


Step 2: Open Actions Panel

Press Option/Alt + F9, or go to Window > Actions to open the Actions Panel.


Step 3: Variables

We'll use three variables, as explained in the comments:


Step 4: Main Function

This is the main function of our counter:


Step 5: Check on Enter Frame

This function calculates the fps on enter frame:


Step 6: Call the Function

Use this single line of code to start the main function:


Step 7: Full Code

The full code should look like this:


Step 8: Document Class Version

Perhaps you'd rather use a document class than timeline code? The following snippet demonstrates how to go about it. Read this Quick Tip if you're not sure how to approach a document class.

You could use this class to embed a FPS counter inside any project like so:

Just copy it all into a new AS file and save it as "FPSDemo.as", then you can use it whenever you like.

Conclusion

This is a basic example of how you can use a frame rate counter function. Have a go, experiment and use it in your own projects.

Thanks for reading!

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.