AS2 to AS3 Buttons
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:
1 2 3 4 | on(press) { //do something } |
For code on the timeline that affects a button (whose instance name we’ll call “button”), we used to do this in AS2:
1 2 3 4 | button.onPress = function() { //do someting; } |
Now in AS3, we have something a little more “wordy”:
1 2 3 4 5 | button.addEventListener(MouseEvent.MOUSE_DOWN,handleClick); function handleClick(e:MouseEvent):void { //do someting; } |
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.
1 |
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.
2 |
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 |
ActionScript 2 to ActionScript 3 Migration Cookbook
I’m starting a new series of mini how-to’s to ease ActionScript 2 migration to ActionScript 3. AS3, as we like to call it, is a much cleaner and more standardized language; not to mention a whole lot faster. Adobe has really been urging everyone who works with Flash to switch to AS3.
So if you’ve been putting migration off, now is the time to learn. These recipe style how-tos will be targeted to mostly Flash Designers and novice to intermediate developers who haven’t made the switch quite yet.
