Thursday, 3 September 2009

Change Frame rate in Actionscript 3

In Actionscript 2 it wasn’t possible to change the frame rate directly at runtime, but now in Actionscript 3 you can change the frame rate of Flash files. The frame rate is the frames per second of the loaded SWF, so the higher the frame rate the faster the SWF will run.

The code for changing the frame rate is very simply and only requires one line of code. Below is the code for the changing the frame rate.

stage.frameRate = ‘yournumber’

Below is an example of changing the frame rate using a simple animation of a moving ball across the stage area.


Change Frame rate in Actionscript 3

Step 1

Open a new Flash AS3 file.
Select the Oval tool and create a simple circle on the stage like below, then place the circle shape at the left edge of the stage.




Step 2


Convert your circle into a movie clip (F8) and give the following instance name: circle_mc




Step 3

On the timeline insert a key frame (F5) at frame 50, and then right click anywhere in between the 1st and 50th frame and select ‘Motion Tween’.

Select the 50th frame and drag the ball movie clip to the right side of the stage.




Step 4

On the timeline create a new layer called buttons. This layer will contain the buttons which will increase and decrease frame rate. Select Window > Components and drag two button components onto the stage. Then change the button labels to ‘decrease’ and ‘increase’.

Give your buttons the following instance name accordingly: decrease_btn and increase_btn.


Step 5

On the time create a new layer called ‘display FR’. This layer will contain a dynamic text box which will display the frames rate.

Select the text tool with dynamic text and drag and text box on the stage like below:



Give your dynamic text box the instance name: showFR_txt.


Step 6

On the time create a new layer called ‘Actions’. Select the first frame and hit F9 to open up the actions panel and enter the following code:


//Event listeners for the buttons with the mouse click event, and
//an event listener on the stage with the enter frame event.
decrease_btn.addEventListener(MouseEvent.CLICK, decreaseFR);
increase_btn.addEventListener(MouseEvent.CLICK, increaseFR);
stage.addEventListener(Event.ENTER_FRAME, showFR);

//Displays the current frame rate on the dynamic text box.
function showFR(event:Event):void {
showFR_txt.text=String(stage.frameRate)+"FPS";
}

//Increase the frame rate by 1fps. The frame rate is limited to 80.
function increaseFR(event:MouseEvent):void {
if (stage.frameRate < 80) {
stage.frameRate +=1;
}
}
//Decrease the frame rate by 1fps.
function decreaseFR(event:MouseEvent):void {
if (stage.frameRate > 1) {
stage.frameRate -=1;
}
}


Step 7

Test your movie clip Ctrl + enter. Now press the increase and decrease buttons and you should see the frame rate go up and down.



You should now be able to change the frame rate in Actionscript 3.

6 comments:

Kev 1 December 2009 22:26  

Hey i was wondering if it's possible to actually change the frame rate of 1 movieclip instead of the whole stage?

iliketo 2 December 2009 04:34  

@Kev

Movie clips play at the same frame rate as the stage, so it is not possible.

BR 22 February 2010 11:30  

i am new to AS. instead of keeping buttons and changing framrate, is it possible t change framerate say till frame 50 it goes on 20 fps and from there it changes to 30. is it possible?

I am actually importing a sequence of images, so need some part to play faster and some slower.
It wud be great if some one can help.


Thanks

iliketo 23 February 2010 01:43  

Try adding the code into the frame you want to change the frame rate of.

Inge 23 February 2010 05:15  

Please help!
I did exactly what you told..
but it doesn't work..
error about <80)>1) { ..
what did i wrong?

iliketo 23 February 2010 07:16  

@Inge

There is some coding missing, i will update it.

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP