Migrating from AS2 to AS3

Presented by Rich Hauck

Rather than get into the complexities of object oriented programming or exhaustively pointing out the differences between ActionScript 2.0 and AS3 (as many other online resources have already done, see bottom of the page), I am focusing on the basics using primarily the Flash CS3 IDE, have assembled a few points that are of interest to me along with simple FLA examples that are free for any use. Happy coding!

- Rich

Quick Note on Data Types

The base datatypes like String, Array, and Number are still available, however, Number has been broken down into the following datatypes:

DataType Range
Number 4.9406564584124654e-324 to 1.79769313486231e+308
int -2147483648 to 2147483647
uint 0 to 4294967295

Quick Notes on Functions

Assigning Defaults

In AS2, if you wanted to ensure a function's argument met a certain range, it typically meant that you'd need to do testing of the value within the function. In Actionscript 3, default values for parameters can now be assigned, like so:

AS2 AS3
function myFunction(num:Number):Void {
if (num < 0) {
num=1;
}
}
function myFunction(num:Number = 1):void{}

Using the wildcard *

You can now use * as a wildcard value, meaning it will accept or return any datatype.

Example of a function that accepts any datatype as an argument and returns any datatype.

function myFunction(val:*):*{
return val;
}

Using the ...(rest) parameter

AS2 AS3
function myFunction(str:String,bool:Boolean,num:Number):Void{
trace(str + ", " + bool + ", " + num);
}
myFunction("a",true,0);

- or -

function myFunction(obj:Object):Void {
trace(obj.str + ", " + obj.bool + ", " + obj.num);
}
var myObj:Object=new Object ;
myObj.str="a";
myObj.bool=true;
myObj.num=0;
myFunction(myObj);
function myFunction(... statements):void {
trace(statements.length + ": " + statements);
}
myFunction("a",true,0);

Working with Display Objects

Here are some of the common DisplayObjects used in Flash:

Properties in ActionScript

For the most part, properties for DisplayObjects in AS3 have remained the same with the exception that the underscore has been taken out of the property name.

AS2 AS3
_name name
_x x
_y y
_width width
_height height
_rotation rotation
_xscale (range 0 to 100+) scaleX (range 0 to 1+)
_yscale (range 0 to 100+) scaleY (range 0 to 1+)
_alpha (range 0 to 100+) alpha (range 0 to 1+)
_xmouse mouseX
_ymouse mouseY

Working with Dynamic Display Objects

Attaching a MovieClip from the Library in Flash CS3


Download Source (ZIP)
Here's the Code:
var myCircle_mc:circle_mc = new circle_mc();
myCircle_mc.x = 100;
myCircle_mc.y = 100;
addChild(myCircle_mc);

Loading an External Bitmap/ Creating a Preloader

Events Model

In Actionscript 2.0, if you wanted your application to react to an event you would typically author a function to trigger based on a particular eventHandler like onRelease or onEnterFrame. This would often lead to scope issues, where a MovieClip's event handler might target something internal to itself or outside of itself--yet the Actionscript to target either of these items would look as if they were of the same scope.

Example:

mc.onEnterFrame = function():Void{
this._rotation++; // refers to mc's rotation
_rotation++; // refers to _root movie's rotation
}

On a more advanced level, Flash OOP developers would use classes like Delegate or Proxy to allow their classes to trigger event handlers. Some would even build frameworks using the object-oriented Observer pattern to register event listeners. In ActionScript 2.0, CS V2 components had an addEventListener() method, however, common building blocks like MovieClip and Button did not.

Fortunately, the events model for Actionscript 3.0 was revisited and rewritten from the ground up. Common building blocks used in Flash like MovieClip, Sprite, SimpleButton (the successor to AS2's Button), and TextField are all subclasses of InteractiveObject, which in turn is a subclass of DisplayObject and ultimately EventDispatcher, which has methods like addEventListener() and dispatchEvent().

The event model and event handling have been rewritten in Actionscript 3.0 from the ground up; now every Gone are the days of having to use the Delegate class (or Proxy if you were fortunate enough to discover it), as well as the days where you wished that addEventListener() was a method not exclusive to AS2 components.

Example of Button events in AS3


Download Source (ZIP)

These events that are dispatched and listened for are subclasses of flash.events.Event. Here's a breakdown of common eventHandlers in AS2 and their equivalent in AS3.

AS2 AS3
onPress "mouseDown" (MouseEvent.MOUSE_DOWN)
onRelease "mouseUp" (MouseEvent.MOUSE_UP), "click" (MouseEvent.CLICK)
onReleaseOutside "mouseUp" (MouseEvent.MOUSE_UP)
onRollOver "mouseOver" (MouseEvent.MOUSE_OVER), "rollOver" (MouseEvent.ROLL_OVER)
onRollOut "mouseOut" (MouseEvent.MOUSE_OUT), "rollOut" (MouseEvent.ROLL_OUT)
onDragOver "mouseOver" (MouseEvent.MOUSE_OVER), "rollOver" (MouseEvent.ROLL_OVER)
onDragOut "mouseOut" (MouseEvent.MOUSE_OUT), "rollOut" (MouseEvent.ROLL_OUT)
mouseDown "mouseDown" (MouseEvent.MOUSE_DOWN)
mouseUp "mouseUp" (MouseEvent.MOUSE_UP)
mouseMove "mouseMove" (MouseEvent.MOUSE_MOVE)
onKeyDown "keyDown" (KeyboardEvent.KEY_DOWN)
onKeyUp "keyUp" (KeyboardEvent.KEY_UP)
onReleaseOutside NONE :(
 

"doubleclick" (MouseEvent.DOUBLE_CLICK)
(unavailable in AS2)

onEnterFrame and Intervals in AS3

Download Source (ZIP)

getURL in AS3

Download Source (ZIP)

Depth Management

Depth management in ActionScript 3.0 is much different than in previous versions. Must be contiguous

AS2 - Skipping depths was acceptable AS3 - Displayed objects must be in order
Level 1 Level 1
Level 2 Level 2
  Level 3
  Level 4
Level 5 Level 5
   
Level 7  

Example of Depth Management in AS3


Download Source (ZIP)

OOP in AS3

Document class

In Actionscript 2.0, many developers would import their Main application class in frame 1 of the timeline. In Flash CS3, this can now be accomplished by defining the Document Class in the properties menu. The Document class serves as the main class for the SWF and therefore must be a subclass of MovieClip.

Package change

AS2 AS3
class com.hauckinteractive.Main {
/**
* Constructor.
*/
private var _timeline:MovieClip;
function Main(mc:MovieClip) {
_timeline=mc;
}
}
package com.hauckinteractive{
import flash.display.MovieClip;
public class Main extends MovieClip {
/**
* Constructor
*/
public function Main() {
}
}
}

Access

keyword scope
public accessible by all
private accessible only within class--convention is to use underscore for private (EXAMPLE: var _myVar)
protected accessible only to class and subclasses
internal accessible only to classes within the same package
static

used to make methods and properties accessible from a direct class call rather than a class instance

Example: com.package.MyClass.doSomething();

const constant--used for a value that doesn't change--convention is to set constant values in all caps (EXAMPLE var MYCONSTANTVAR)

Additional Resources