I know the transition can be hard. We got used to the ease of use ActionScript 2 had, but honestly, it became problematic later on, especially if designers were involved in the project and found it easy to add code as they saw fit.
So, this article takes the basic operations we all did in AS2 and brings them to AS3.
So what happened to my on(release) or something.onRelease statements?
The on(EVENT) clause was an AS1 relic, but AS2 kept it for backwards functionality. The biggest problem with this is that it made the code appear all over the project and finding it could prove to be a big challenge. The Object.ONEVENT clause proved to be better but people could still define the function afterwards and if it was big enough it could be difficult to read the code.
//AS1on(release) { trace(this); //would return _level0.myButton (or _level0.instanceXX if it didn't have a name)}
For AS3, we need listener objects, the listener objects are "listening" to the object they are assigned to, waiting for them to execute an event. If the listener is expecting a certain type of event, it will fire the function assigned to it.
//AS3myButton.buttonMode = true; //makes the cursor a hand, otherwise it shows the default arrowmyButton.addEventListener(MouseEvent.MOUSE_DOWN, buttonPressed);function buttonPressed(event:MouseEvent):void { trace(event.currentTarget); //returns "[Object MovieClip]" trace(event.currentTarget.name); //returns "myButton"}
AS2 was extremely forgiving. You could not define any variable and the code would still work. This was useful but was also a nightmare when a project grew too much. It's not the case anymore for AS3, where you must define the variable and it's type (although, there's still the wildcard "*" for any type).
We never defined the type of anything, we assumed that total was a string, but what if we wanted to actually add and not just concatenate? In AS3 we can't do that anymore.
So, what happened with this in AS3? Well it got a pretty major overhaul.
//AS3myButton.addEventListener(MouseEvent.MOUSE_DOWN, buttonPressed);function buttonPressed(event:MouseEvent):void { var urlRequest:URLRequest = new URLRequest("http://www.experts-exchange.com"); navigateToURL(urlRequest);}
This is just a small tidbit of AS3 to give you a starting point. There are countless other changes like the removal of attachMovie, loadMovie, createEmptyMovieClip, and many others that I'll write about in the next article.
Comments (1)
Commented: