Forum Topic: As3: Fps Display

(537 views • 7 replies)

This topic is 1 page long.

<< < > >>
None

StaliN98

Reply To Post Reply & Quote

Posted at: 1/26/09 02:27 PM

StaliN98 LIGHT LEVEL 10

Sign-Up: 07/27/07

Posts: 690

AS3:Main
This is a simple little gadget you can easily place in your games, to see what speed it is running at. This was an experiment for me, to try out AS3 and tone my OOP.
Previous reading:
Object Orientated Programming
Timer Object
Text Fields
Events

The FPS display is a custom class I created. So create a new .as file in the same directory as your flash that you wish to include it in, and name it "FPS".
You first need to import some packages that we will need:

package {

	import flash.display.*;
	import flash.text.*;
	import flash.utils.*;
	import flash.events.*;

The display will simply be used to draw the background for the display, the text for showing the FPS, utils for the timer, and events for, well, events.
Next we must start the class proper, and define some variables / objects:

public class FPS extends Sprite {

	private var frameR:int = 0;
	private var count:int = -1;
	private var textBx:TextField = new TextField();
	private var textFm:TextFormat = new TextFormat("Arial",12);

"frameR" will be the current FPS. "count" will count how many frames pass every second. "textBx" is the text field that will display the FPS. Finally, "textFm" will format the text field, so it isn't Times New Roman.
Following this is the constructor, that runs when a FPS object is created:

public function FPS (stage:Object, xx:int, yy:int):void {

	var timer:Timer = new Timer(1000,0);
	timer.addEventListener (TimerEvent.TIMER, addCount);
	stage.addEventListener (Event.ENTER_FRAME , addFrame);

	this.graphics.lineStyle (1,0,1);
	this.graphics.beginFill (0xFFFFFF,1);
	this.graphics.drawRect (xx,yy,48,16);
			
	addChild(textBx);
			
	textBx.x = xx;
	textBx.y = yy;
	textBx.width = 48;
	textBx.height = 16;

	timer.start();

}

The parameters are the Stage object, desired x and desired y position respectively. A timer object is created, with an interval of one second (1,000 milliseconds). A listener is added, that runs the function "addCount" every second. The stage object then has a listener added, that runs "addFrame" every time a frame passes. A box is drawn as a background for the FPS display, and the text field is moved into position. Finally, the timer object starts ticking.
Lastly, the functions referred to above need to be created:

private function addCount (TimerEvent):void {

	frameR = count;
	count = -1;
			
	textBx.text = "FPS "+String(frameR);
	textBx.setTextFormat(textFm);
			
}

private function addFrame (Event):void {

	count++;

}

"addCount" resets the counter, and then displays the FPS in the text field. "addFrame" adds to the variable "count" every frame. Oh, and you might want to add two "}"'s at the end.
Well, that's it. This may not be the simplest way to do this, but I used the creation of this to teach myself more about AS3. Thanks for reading.


None

BillysProgrammer

Reply To Post Reply & Quote

Posted at: 1/26/09 02:28 PM

BillysProgrammer LIGHT LEVEL 16

Sign-Up: 09/17/08

Posts: 2,260

Its pretty good, I dont do as3 but I found it interesting. Also, for the new ones, you may want to explain the * symbol. (ALL)


None

Deathcon7

Reply To Post Reply & Quote

Posted at: 1/26/09 02:34 PM

Deathcon7 NEUTRAL LEVEL 21

Sign-Up: 10/01/03

Posts: 5,870

At 1/26/09 02:28 PM, BillysProgrammer wrote: Its pretty good, I dont do as3 but I found it interesting. Also, for the new ones, you may want to explain the * symbol. (ALL)

The * symbol is the same as everywhere else, it's a wild card.

Also, I like the previous readings section. Very good touch!


None

BillysProgrammer

Reply To Post Reply & Quote

Posted at: 1/26/09 02:36 PM

BillysProgrammer LIGHT LEVEL 16

Sign-Up: 09/17/08

Posts: 2,260

I know, but A lot of people do not know that unless they have knowledge of other programming languages.


None

Patcoola

Reply To Post Reply & Quote

Posted at: 1/26/09 03:02 PM

Patcoola LIGHT LEVEL 46

Sign-Up: 03/07/03

Posts: 2,087

At 1/26/09 02:27 PM, StaliN98 wrote: The FPS display is a custom class I created. So create a new .as file in the same directory as your flash that you wish to include it in, and name it "FPS".

ya that works, but

why is count = -1; shouldn't it be zero.
and do you have too set the font of the field every time you display the FPS.

Website | Blog | Buy T-Shirts | Flash | Audio

BBS Signature

None

StaliN98

Reply To Post Reply & Quote

Posted at: 1/26/09 03:17 PM

StaliN98 LIGHT LEVEL 10

Sign-Up: 07/27/07

Posts: 690

The count is -1 because the addFrame function will occur on the same frame as the count clearing, so it adds one more than it should. If you make a blank flash with a framerate of 48, it will display 49.
There is something like defaultFormat or something, but I haven't looked it up (for the text field).


None

Patcoola

Reply To Post Reply & Quote

Posted at: 1/26/09 04:27 PM

Patcoola LIGHT LEVEL 46

Sign-Up: 03/07/03

Posts: 2,087

At 1/26/09 03:17 PM, StaliN98 wrote: adds one more than it should. If you make a blank flash with a framerate of 48, it will display 49.

thats interesting because it shouldn't, have you tried testing on different platforms.
have you tried making a MC add 120 blank frames insert a silent audio file to loop at streaming and let it loop in the background.

Website | Blog | Buy T-Shirts | Flash | Audio

BBS Signature

None

StaliN98

Reply To Post Reply & Quote

Posted at: 1/26/09 04:30 PM

StaliN98 LIGHT LEVEL 10

Sign-Up: 07/27/07

Posts: 690

Well I tested it on a 3D engine I am developing, and even when constantly rendering the display it would be about 48 - 49. So I think I found the problem (it still adding on the frame it updates).


All times are Eastern Standard Time (GMT -5) | Current Time: 07:33 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!