In ActionScript 3, Adobe got rid of attatching code directly to the MovieClip or Button you were working with on the the timeline.
So no more of this:
[cc lang="actionscript"]
on(press)
{
//do something
}
[/cc]
For code on the timeline that affects a button (whose instance name we’ll call “button”), we used to do this in AS2:
[cc lang="actionscript"]
button.onPress = function()
{
//do someting;
}
[/cc]
Now in AS3, we have something a little more “wordy”:
[cc lang="actionscript3"]
button.addEventListener(MouseEvent.MOUSE_DOWN,handleClick);
function handleClick(e:MouseEvent):void
{
//do someting;
}
[/cc]
One thing to understand about ActionScript 3 is that it is an event driven language. You listen for events, events get dispatched, and you handle them appropripately. I’ll break down the code for you.
[cc lang="actionscript3" first_line="1"]
button.addEventListener(MouseEvent.MOUSE_DOWN,handleClick);
[/cc]
What we’re saying on line 1 is here, we have a button that will listen for MouseEvent.MOUSE_DOWN events, and if that happens run the handleClick function.
[cc lang="actionscript3" first_line="2"]
function handleClick(e:MouseEvent):void
[/cc]
Here, we’re just defining the function “handleClick.” It accepts an argument of type MouseEvent and :void means it doesn’t return anything.
As you can see, AS3 for buttons isn’t too difficult, just a little wordier. Below, I’ve listed the old way of handling buttons with their new event listeners. Just swap out the MouseEvents for what you need when you add the event listener.
| AS2 | AS3 |
| button.onPress | MouseEvent.MOUSE_DOWN |
| button.onRelease | MouseEvent.MOUSE_UP |
| button.onRollOver | MouseEvent.MOUSE_OVER |
| button.onRollOut | MouseEvent.MOUSE_OUT |
Great Job, Ive been looking for something like this.