Showcase and discover digital art at yex

Follow Design Stacks

Subscribe to our free newsletter to get all our latest tutorials and articles delivered directly to your inbox!

Dynamically creating instances of a class

Dynamically creating instances of a class

You needn’t always drag an instance of a symbol onto the Stage during authoring time; you can also create new instances of a class in the Library using ActionScript by using the new operator.

Using the new operator

Previous versions of ActionScript forced you to create new MovieClip or TextField instances by using methods such as MovieClip.attachMovie(), MovieClip.createEmptyMovieClip(), or MovieClip.createTextField(). In ActionScript 3.0, you can create new MovieClip, TextField, Sprite, and Video instances—or your own custom classes—by calling new Ball(), as seen in the following example:

  1. Delete the ball symbol from the Stage in the flancyBall.fla document and add the following ActionScript to the main Timeline:

    var b1:Ball = new Ball();
  2. Press Control+Enter (Command+Return) to test the SWF file. Notice that nothing appears on the Stage, but the following text appears in the Output panel: “ball created: instance1”. Although Flash creates a new instance of the Ball class as requested, it isn’t visible on the Stage because you didn’t add the symbol to the display list using the addChild() method.
  3. Modify the code from Step 1 to add the following line of code, which adds the b1 instance to the display list:


Next you need to use a document class, which allows you to move code off of frame 1 of the timeline and place it in an external file instead. This is similar to associating a class with a symbol, as shown in previous examples.

Using the Document Class text box

The next example demonstrates how you can move code off the Timeline and place it in an AS file:

  1. Delete the code in frame 1 of the fancyCircle.fla document.
  2. Create a new ActionScript document and save it as in the same folder as the fancyCircle.fla document.
  3. Add the following ActionScript code to

    package {
    import flash.display.MovieClip;
    public class BallDocumentClass extends MovieClip {
    private var tempBall:Ball;
    private var MAX_BALLS:uint = 10;
    public function BallDocumentClass() {
    var i:uint;
    for (i = 0; i < MAX_BALLS; i++) {
    tempBall = new Ball ();
    tempBall.scaleX = Math.random();
    tempBall.scaleY = tempBall.scaleX;
    tempBall.x = Math.round(Math.random() *
    (this.stage.stageWidth - tempBall.width));
    tempBall.y = Math.round(Math.random() *
    (this.stage.stageHeight - tempBall.height));
  4. Save and close the AS file and open the fancyBall.fla document.
  5. Type BallDocumentClass in the Document Class text box in the Property inspector and then save the FLA file.
  6. Select Control > Test Movie to preview the SWF file. You should see 10 instances of the ball symbol at various shapes and sizes on the Stage.

Using the Document Class text field allows you to place ActionScript code in external files instead of within a frame on the main Timeline, reuse the code between numerous FLA files, and check the ActionScript code into a Concurrent Versioning System (CVS)—making it much easier to share code within a large team.