Jul 02

There are some small differences in syntax when dynamically attaching library objects in ActionScript 2.0 and ActionScript 3.0.

In this post, I will provide a very simplified example in both ActionScript 2.0 and ActionScript 3.0.

ActionScript 2.0 Example:

You will need to make sure the library object that you are trying to attach has been configured, to do this you need to right click on the object in the library, next select properties and then check the “Export for ActionScript” and “Export in first frame” options. Make sure to enter an Identifier name for the library object; the Identifier name is used to reference the library object in when you try to use it in the ActionScript code.

In this example we will use “myClip” as the Identifier name.

Below is the ActionScript 2.0 code that will dynamically create 5 instances of the myClip object.

1
2
3
4
5
6
7
8
9
10
11
12
13
var numberOfClips:Number = 5; 
var xStart:Number = 0; 
var yStart:Number = 0; 
var xVal:Number = xStart; 
var xOffset:Number = 2; 
for (var i=0;i<numberofclips;i++) 
{ 
     this.attachMovie("myClip","myClip"+i,this.getNextHighestDepth()); 
     this["myClip"+i]._y = yStart; 
     this["myClip"+i]._x = xVal; 
     xVal = this["myClip"+i]._x + this["myClip"+i]._width + this.xOffset; 
     this["myClip"+i].label_txt.text = i.toString(); 
}

ActionScript 3.0 Example:

You will need to make sure the library object that you are trying to attach has been configured, to do this you need to right click on the object in the library, next select properties and then check the “Export for ActionScript” and “Export in first frame” options. Make sure to enter an Class name for the library object; the Class name is used to reference the library object in when you try to use it in the ActionScript code. You don’t need to worry about the Base class, Flash will set a default Base class.

If you are attempting to attach a custom class that you created, you will need to use the Class name of the your custom class; you may also need to set the Base class if your custom class is derived from another custom class that you have created.

When you check the “Export for ActionScript” option, you will likely get a warning telling you that there is no class file found; for this basic example you can just click OK.

Below is the ActionScript 3.0 code that will dynamically create 5 instances of the myClip object.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var numberOfClips:Number = 5; 
var xStart:Number = 0; 
var yStart:Number = 0; 
var xVal:Number = xStart; 
var xOffset:Number = 2; 
for (var i:Number=0;i<numberofclips;i++) 
{ 
     var mc:myClip = new myClip(); 
     mc.name = "myClip"+(i+1); 
     this.addChild (mc); 
     mc.y = yStart; 
     mc.x = xVal; 
     xVal = mc.x + mc.width + this.xOffset; 
     mc.label_txt.text = (i).toString(); 
}

Here are the example source files.

Feb 18

I’m currently architecting an ActionScript 3.0 Framework that will take advantage of OOP principles & a MVC design pattern.

In an attempt to automate as much of the work as possible, I started looking for tools that could generate AS3.0 code from UML diagrams. I found an excellent blog post by Sean Moore, with a listing of several UML diagramming tools with AS3.0 code generation.

I decided to use StarUML, which was mentioned in one of the comments. To enable AS3.0 code generation in StarUML, you will need to download the AS3.zip file & then copy the contents of the AS3.zip file to this directory location C:\Program Files\StarUML\modules\staruml-generator\templates\.

The template.cot file is a JavaScript file that StarUML uses to generate the AS3.0 code. I noticed that the AS3.0 code being generated did not include the package namespace of the parent class or the implemented interfaces, so I modified the template.cot file so that it generates the import statements of the missing namespaces. My colleague also made some revisions to the template.cot file so that it would not include the access control modifiers for the operations created in the interface AS files.

To install the modified template.cot file you can simply overwrite the C:\Program Files\StarUML\modules\staruml-generator\templates\AS3 folder with the contents of this AS3.zip file.

Here are the steps that you’ll need to follow to generate the AS3.0 code once you have the AS3.zip contents installed.

1. When you start StarUML, it will ask you for the approach you wish to use. Leave it as Default Approach and press OK.

2. Once you have created your UML diagrams and want to generate the AS3.0 code, go to the Tools menu and select “StartUML Generator …”.

3. You will be presented with a dialog asking for the template type that StarUML should use for code generation; check the AS3.0 template and click Next.

4. Select the destination directory that StarUML will place the generated AS3.0 code and then press Next.

5. Press the Generate button to begin the code generation.

6. Presto! Code generation complete. Click on Finish and then go to the destination folder that you specified in Step 4 to view your results.

Here is a sample project that I created that you can use as a starting reference point.