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 { |
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{
- or - function myFunction(obj:Object):Void { |
function myFunction(... statements):void { |
Working with Display Objects
Here are some of the common DisplayObjects used in Flash:
- Sprite (flash.display.Sprite) – think of this as a MovieClip with only one frame
- Shape (flash.display.Shape) – similar to a Sprite but with no interactivity
- Bitmap (flash.display.Bitmap) – For displaying bitmaps
- Loader (flash.display.Loader) – DisplayObject used to contain loaded content (SWFs and images)
- SimpleButton (flash.display.SimpleButton) - Successor to Button from AS2
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 |
- root (formerly _root) is now achild of stage
- root and global are properties of DisplayObject, assigned when added to stack
Working with Dynamic Display Objects
- A Linkage Base class is now defined
- No duplicateMovieClip() in AS3
- Calls like createemptymovieclip() or attachMovie() have been replaced by instantiation:
var mySprite:Sprite = new Sprite();
addChild(mySprite); - BitmapData now linkable
Attaching a MovieClip from the Library in Flash CS3
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
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) |
onEnterFrame and Intervals in AS3
getURL in AS3
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 |
- swapDepths()
- _level is gone
Example of Depth Management in AS3
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 { |
package com.hauckinteractive{ |
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) |