O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Instantiating Flash Authoring Symbols via ActionScript

In the earlier section "Linked Classes for Movie Clip Symbols," we learned that instances of a symbol can be created manually in the Flash authoring tool by dragging the symbol's name from the Library to the Stage of a timeline. Instances of symbols that are exported for ActionScript can also be created directly through code, using the standard new operator.

For example, earlier, we linked the star symbol to the Star class. To create an instance of the star symbol through code, we use the following expression:

new Star()

To add an instance of the star symbol to the main timeline of sky.swf at runtime, we would use the following code in either a frame script on the main timeline, or a method in the Sky class:

var star:Star = new Star();
addChild(star);

The following code creates 50 star instances, and positions them randomly on screen:

var sky:Array = new Array();
for (var i:int = 0; i < 50; i++) {
  sky.push(new Star());
  sky[i].x = Math.floor(Math.random()*550);
  sky[i].y = Math.floor(Math.random()*400);
  addChild(sky[i]);
}

Note that ActionScript code can be used to create instances of any symbol that is exported for ActionScript, whether or not the symbol has a custom-defined linked class. That is, the class specified on the Linkage Properties dialog need not be a custom class (as Star is). As we learned earlier, when the class specified on the Linkage Properties dialog is not found in the classpath, ActionScript automatically generates ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required