<

Go Premium for a chance to win a PS4. Enter to Win

x

From ActionScript 2 To ActionScript 3.0

Published on
5,764 Points
2,664 Views
1 Endorsement
Last Modified:
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.

 
//AS1
on(release) {
    trace(this); //would return _level0.myButton (or _level0.instanceXX if it didn't have a name)
}

Open in new window


//AS2
myButton.onRelease = function() {
    trace(this); //would return something like _level0.myButton
}

Open in new window


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.

 
//AS3
myButton.buttonMode = true; //makes the cursor a hand, otherwise it shows the default arrow
myButton.addEventListener(MouseEvent.MOUSE_DOWN, buttonPressed);

function buttonPressed(event:MouseEvent):void {
    trace(event.currentTarget); //returns "[Object MovieClip]"
    trace(event.currentTarget.name); //returns "myButton"
}

Open in new window



Undefined Variables?

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).

 
//AS2
total="";
for(i=0;i<=5;i++) {
	total+=i;
}
newvar = total;
trace(total); //returns 012345
trace(newvar); //returns 012345

Open in new window


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.

 
//AS3
var total:String = "";
var i:Number = 0;
var newvar:String = "";
for(i=0;i<=5;i++) {
	total+=i;
}
newvar = total;
trace(total); //returns 012345
trace(newvar); //returns 012345

Open in new window


We have to define the type of the variable before being able to continue.


And what about getURL?

A lot of people have asked me about that one. It was a simple yet useful function we used to have.

 
//AS2
myButton.onRelease = function() {
   getURL("http://www.experts-exchange.com");
}

Open in new window


So, what happened with this in AS3? Well it got a pretty major overhaul.

 
//AS3
myButton.addEventListener(MouseEvent.MOUSE_DOWN, buttonPressed);

function buttonPressed(event:MouseEvent):void {
	var urlRequest:URLRequest = new URLRequest("http://www.experts-exchange.com");
	navigateToURL(urlRequest);
}

Open in new window



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.

-V
1
Comment
1 Comment
 
LVL 3

Expert Comment

by:mharfouch
Good article.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Join & Write a Comment

The goal of the tutorial is to teach the user how to how to record live broadcast.
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.
Suggested Courses

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month