Migrating from as2 to as3

Jul 28, 03:03 PM |

Here is the help for those who want to migrate from ActionScript 2 to AS3 (if you haven’t yet done it) with minimal pain.

I assume you work in Flash IDE, but most of what I’ll say here is applicable also to FlashDevelop or Flash Builder environment.

Choose “start a project” at the beginning.

Just use “create new project” option before starting an application. In Flash IDE, you can skip it if it’s going to be very simple one.

Declare all variables.

Before you can use a variable, you must first declare it, e.g.:

var v:Number;

Even function needs to have a type if it has to return a value:

function anyFunction(a:Number, b:Number):Number { //…

If you want to avoid type control (which wasn’t present in as2), declare everything as “any type”:

var v:*;
function anyFunction(a:* , b:* ):* { ///…

Forget the as2 underscores

See the as2 to as3 differences:

_parent, _root……………parent, root
_x, _y………………………x, y, z(!)
_alpha (0 to 100)………..alpha (0 to 1)
_xscale (0 to 100)……….scaleX (0 to 1)

The movieclip concept has been changed completely

Suppose you are in the main timeline. To create here a movieclip with as3, you can no longer use the easy as2 function

duplicateMovieClip(“mc”, “mc1”, this.getNextHighestDepth());

The as3 movieclip creation concept is more similar to as2 attachMovie function (which is also no longer applicable in as3). You first create a movieclip with the Flash graphic tools, and give it a linkage in the library, say, “Figure”. This way you created an embedded class (i.e. not-external to your fla-file) with the name Figure, which is a descendant of flash.display.MovieClip class and owns its properties (but can also have additional properties). Now you can type:

var m:Figure=new Figure();

What happened? You created an instance of Figure, gave it some parameters, and, by using function addChild, placed it to the main timeline (or wherever is your “this”). Now it resides at the top (greatest) layer depth of the main timeline.
If you want to swap two movieclip depths, no more use

swapDepths(mc1, mc2); //as2

Instead, you use

swapChildren(getChildByName(“mc1”), getChildByName(“mc2”)); //as3


swapChildren(getChilAt(a), getChildAt(b)); //a and b are integers denoting layer depths

Forget on(release).

Use addEventListener (and removeEventListener) instead. See my related article for a little more details. There are no longer actions attached to movieclips or buttons. You must write all the code in the frame timeline, if you write it directly to flash file.

Use external “data storage” file

There are difficulties in as3 with targetting variables in different movieclip instances. (See my preceding article.) You can overcome them by creating one .as file which will contain global variables. It’s also convenient for the case you’ll have more .as files in your project. Here is the process:

1.Create new actionScript class, place it in the same folder where your main fla file resides, and call it MyGlobals.as

2.Write the following code inside and save it. (.as files in Flash IDE must be saved to count their latest changes in the swf compilation.)

		package  {	
		  public class MyGlobals {
			public var str:String;
			public var var1:Number;
			public var var2:Number;
			//...other variables declarations
			private static var myGlobals : MyGlobals;
			public static function getInstance() : MyGlobals {
			  if ( myGlobals == null ) {
				myGlobals = new MyGlobals();
			  return myGlobals;
			public function MyGlobals() {
			// constructor code

The function getInstance() will assure you only use one instance of the MyGlobals class. (Note that classes start with Capital Letter, while variables start with small one.)

3.You will then access it from any level/movieclip of your fla-file (or from any .as file of this project) using this code:

import MyGlobals;
var myGlobals:MyGlobals=MyGlobals.getInstance();
myGlobals.var1 = myGlobals.var2 + 1;

There is still more to learn, but the main differences were covered here, so from now on you shouldn’t fear to start quickly using as3.





Web design (c) ThunderBird Animations, 7/2011. Powered by Textpattern.