Button & Movieclip actions in Action Script 3
Introduction
The way Action Script deals with events on buttons and movie clips is totally different than the old one we used to do with Action Script 2, but generally it is more professional and saves time and effort if you understood it well.
The new Action Script 3 is showing a new trend on the Action Script language by moving to the Object oriented programming concept. However, I wanted in this article to show for Flash designer how the new AS3 deal with movie clips and button events since it is one of the AS basics that any Flash designer or animation must be aware of it.
Requirements:
Adobe Flash CS3 professional
To complete this tutorial, you will need basic knowledge of using Adobe Flash CS3 Professional.
Adding Event Listener:
The new method does not allow you to add action to movie clip or button, so all our action will be on the timeline frame. This way is better since it unify the location of the code.
In this part, we will start by adding the Event Listener. The Event listener mean as you can see from the words that it will listen to the mouse action such as mouse release, mouse roll-over or mouse roll-out. When this event happens, it executes the function that includes what you need to object to do when clicked.
However, we will understand how it works more from the following example:
- Open a new Action Script 3 file.
- Create a rectangle and convert it to a button.
- Select the button and name it act_btn from the instance field in the properties panel.
- Create a new layer and name it Action Script, and press F9 to open the Action Script panel.
- Make sure you are in the expert mode where you can write you code manual.
- Write the following code in the Action Script panel:
act_btn.addEventListener (MouseEvent.CLICK, onClick);- The first part of the code (act_btn.addEventListener) listens to the event that will be applied to act_btn symbol.
- MouseEvent.CLICK shows that the event is when the user clicks the mouse button.
- onClick is the name of the function that will include what will happened when the user click the button.
The Handler Function
Here, we will see how to write the function that will include the action hat will happen when the user clicks on the button as following:
Write the following code after the Listener code:
function onClick ( event:MouseEvent):void
{
event.target.y -= 5;
event.target.rotation += 45;
}
- In this code we created the function that will have the event inside it and name it onClick.
- ( event:MouseEvent) means that the function will listen to the click event on the instance act_btn.
- The void value means that the function does not return any value.
event.target.y -= 5;
event.target.rotation += 45;
- This part shows that the targeted instance will move on the stage by -5 and rotate by +45 degrees.
Adding Multi Events to a button:
You can add more than one event to a button by duplicating the above steps. For example if you like the button to do an action when roll over, another action when roll out and a third action when clicked, add the following code:
act_btn.addEventListener (MouseEvent.CLICK, onClick);
act_btn.addEventListener(MouseEvent.ROLL_OVER, mcOver);
act_btn.addEventListener(MouseEvent.ROLL_OUT, mcOut);
function mcOut( event:MouseEvent):void
{
event.target.alpha = 1;
}
function mcOver( event:MouseEvent):void
{
event.target.alpha = 0.5;
}
function onClick( event:MouseEvent):void
{
event.target.y -= 5;
event.target.rotation += 45;
}
In this code, the button will turn to alpha 0.5 when the mouse roll over it, return to alpha 1 when the mouse roll out, and move and rotate when clicked.
Adding action to a movie clip:
To add an action to a movie clip, you will need to follow the above steps, but you will need to tell Flash that to deal with this movie clip instance as if it were a button and shows a hand curser when the mouse goes over it. This will be done by adding the
following code at the end of the above code:
Act_btn_Mode = true;
Where to go from here
The differences between the old AS2 and the new AS3 are huge, but it really deserves to learn, it will give you more capabilities and more flexibility. If you are a designer that seeks a better performance to your work and more capabilities, then AS3 will give you what you need and more.
Category: Flash tutorials















I sent this page to my sister to see
I sent this page to my sister to see
I must say that I like it.
I must say that I like it.
I must say that I like it.
I must say that I like it.
oops wrong writing…
must be like this …
act_btn.buttonMode= true;
BTW thx for thr tutorial i’m new for AS3 ….
oops wrong writing…
must be like this …
act_btn.buttonMode= true;
BTW thx for thr tutorial i’m new for AS3 ….
oops wrong writing…
must be like this …
act_btn.buttonMode= true;
BTW thx for thr tutorial i’m new for AS3 ….
Oh, thanks alot for fixing this, i will update it
VERY INTERESTING POST, NICE SITE
VERY INTERESTING POST, NICE SITE
thank you so muchhhhhh!!!
thank you so muchhhhhh!!!
Thanks for this! Much appreciated.
Thanks for this! Much appreciated.
Thanks for this! Much appreciated.
Well… it seems to be a bit complicated. Anyway – thank you for posting this – always it is a chance to try myself! Good
Well… it seems to be a bit complicated. Anyway – thank you for posting this – always it is a chance to try myself! Good
Well… it seems to be a bit complicated. Anyway – thank you for posting this – always it is a chance to try myself! Good
how can i make the button go to a different frame???in as3???
how can i make the button go to a different frame???in as3???
how can i make the button go to a different frame???in as3???
Sorry for the late reply, just add the code gotoAndPlay (frame number); inside the onClick function
Hope this help
Sorry for the late reply, just add the code gotoAndPlay (frame number); inside the onClick function
Hope this help
Sorry for the late reply, just add the code gotoAndPlay (frame number); inside the onClick function
Hope this help
How do you add another action to another button?
even if I name the second button’s instance with act_btn2, I’m getting duplicated function error
How do you add another action to another button?
even if I name the second button’s instance with act_btn2, I’m getting duplicated function error
How do you add another action to another button?
even if I name the second button’s instance with act_btn2, I’m getting duplicated function error
you can copy the following code for changing the frame:
function onClick ( event:MouseEvent):void
{
gotoAndPlay (number); ////give your frame number here .(where do you want to go??)
}
you can copy the following code for changing the frame:
function onClick ( event:MouseEvent):void
{
gotoAndPlay (number); ////give your frame number here .(where do you want to go??)
}
you can copy the following code for changing the frame:
function onClick ( event:MouseEvent):void
{
gotoAndPlay (number); ////give your frame number here .(where do you want to go??)
}
Works wonderful! Thanks for your nice work!,Jul 7, 2010 3:27:14 PM
Works wonderful! Thanks for your nice work!,Jul 7, 2010 3:27:14 PM
Works wonderful! Thanks for your nice work!,Jul 7, 2010 3:27:14 PM
very complicated way to do this. There should be an easier way