Setting up a frame-frequency counter in ActionScript3.0

Source: Internet
Author: User
Tags addchild current time reset time in milliseconds

In this tutorial, you will learn how to measure the current frame frequency in flash and display it.

The importance of frame frequency
Each frame is a static picture, fast and consecutive frames will produce animation, the more frames per second, the animation will be more fluent.

Measuring the frame frequency allows us to detect the process of animation, if necessary can also optimize the animation.

This is useful when you are testing animations in different running environments, such as in a browser, in a computer, in a TV or in a cell phone.

We can use the GetTime () method to measure the frame rate, and immediately create a text field in the stage to output the results.

——————————————————————————–

First step: Create a new document
Open Flash and create a new Flash document (ActionScript 3).

——————————————————————————–

Step Two: Open the action artboard

Perform window > action to open the action panel.

——————————————————————————–

Step Three: Variable

We will use three variables, as follows:

var Starttime:number; Used to calculate relative time
var framesnumber:number = 0; Current frame Frequency
var Fps:textfield = new TextField (); Text field showing actual frame frequency

——————————————————————————–

Fourth Step: main function
This is the main function of the counter:

function Fpscounter (): void
{
StartTime = Gettimer (); Get time in milliseconds when animation starts
AddChild (fps); Add a text field to the stage

AddEventListener (Event.enter_frame, Checkfps); Add frame-by-step listeners and perform checkfps functions
}

——————————————————————————–

Fifth step: Check the frame frequency

This function computes the frequency frame:

function Checkfps (e:event): void
{
var currenttime:number = (Gettimer () –starttime)/1000; Gets the time in seconds when the function executes

framesnumber++; Counter plus 1

if (CurrentTime > 1)//Determine if the current time is greater than 1
{
Fps.text = "FPS:" + (Math.floor (framesnumber/currenttime) *10.0)/10.0); Compute frequency frames and display them in a text field
StartTime = Gettimer (); Reset Time to start
Framesnumber = 0; Reset the number of frames
}
}

——————————————————————————–

Step Sixth: Call the function
Start the main function with the following line of code:

Fpscounter ();

——————————————————————————–

Step Seventh: All code:

All of the code is as follows:

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 ();

——————————————————————————–

Eighth Step: Document class version

You might be more accustomed to using document classes than timeline code. The following sections explain how to use the.

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;
}
}
}
}

You apply this class to embed one of these frequency frame counters in any project:

var Fpsdemo:fpsdemo = new Fpsdemo ();
AddChild (Fpsdemo);

Just copy it to a new as file and save it as "fpsdemo.as."

Conclusion
This is a basic example of using the frame-frequency counter function. Try it and use it in your project.

Thanks for reading.










Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.