


October 31, 2014 – How to create a button in AS3
So, you are migrating from Actionscript 2 to Actionscript 3, and you noticed that the actions panel of the button is missing, you can create the button like in Actionscript 2, but you can't click in the button to define the actions.
First, let's remember how to create a button in Actionscript 2, the image below shows the process:
In Actionscript 3, the button is no longer used, it's considered deprecated. To create a button in Actionscript 3, you will need to use a Movie Clip. So, let's start:
I - Creating a button in Actionscript 3
1) Draw the button on the stage, like you did in Actionscript 2.
2) Now, select your image and convert to a Movie Clip.
3) Now, give a Instance Name to the object on the stage (in our example it will be as3button).
4) Open the Actions Panel of the current frame to start the codes, you will write the actions of the button in the Actions Panel of the frame.
II - Defining the button events (Mouse Over, Click, Mouse Out)
First of all, you need to import the events package:
import flash.events.*;
Now, let's add the event listeners of the button starting with the mouse click event:
as3button.addEventListener(MouseEvent.CLICK, BtnClick);
In this line, you are connecting the click event with the function BtnClick, now, let's define this function:
function BtnClick(e:MouseEvent):void {
// do something
}
To add another events, it's the same process, you will just need to change the MouseEvent.CLICK, to another thing to define the event, you can use: MouseEvent.MOUSE_OVER, MouseEvent.MOUSE_OUT and point to another function.
III - The code
import flash.events.*;
function BtnClick(e:MouseEvent):void {
// do something when clicking in the button
}
function BtnOver(e:MouseEvent):void {
// do something when the mouse is over the button
}
function BtnOut(e:MouseEvent):void {
// do something when the mouse is not over the button
}
as3button.addEventListener(MouseEvent.CLICK, BtnClick);
as3button.addEventListener(MouseEvent.MOUSE_OVER, BtnOver);
as3button.addEventListener(MouseEvent.MOUSE_OVER, BtnOut);
First, let's remember how to create a button in Actionscript 2, the image below shows the process:

In Actionscript 3, the button is no longer used, it's considered deprecated. To create a button in Actionscript 3, you will need to use a Movie Clip. So, let's start:
I - Creating a button in Actionscript 3
1) Draw the button on the stage, like you did in Actionscript 2.
2) Now, select your image and convert to a Movie Clip.

3) Now, give a Instance Name to the object on the stage (in our example it will be as3button).
4) Open the Actions Panel of the current frame to start the codes, you will write the actions of the button in the Actions Panel of the frame.
II - Defining the button events (Mouse Over, Click, Mouse Out)
First of all, you need to import the events package:
import flash.events.*;
Now, let's add the event listeners of the button starting with the mouse click event:
as3button.addEventListener(MouseEvent.CLICK, BtnClick);
In this line, you are connecting the click event with the function BtnClick, now, let's define this function:
function BtnClick(e:MouseEvent):void {
// do something
}
To add another events, it's the same process, you will just need to change the MouseEvent.CLICK, to another thing to define the event, you can use: MouseEvent.MOUSE_OVER, MouseEvent.MOUSE_OUT and point to another function.
III - The code
import flash.events.*;
function BtnClick(e:MouseEvent):void {
// do something when clicking in the button
}
function BtnOver(e:MouseEvent):void {
// do something when the mouse is over the button
}
function BtnOut(e:MouseEvent):void {
// do something when the mouse is not over the button
}
as3button.addEventListener(MouseEvent.CLICK, BtnClick);
as3button.addEventListener(MouseEvent.MOUSE_OVER, BtnOver);
as3button.addEventListener(MouseEvent.MOUSE_OVER, BtnOut);
© PlayGB.com - Free online games - Blog
