Accessing Manually Created Symbol Instances

At runtime, every instance of any symbol placed on any timeline in a .fla file automatically becomes a display child of the ActionScript object representing that timeline.

For example, in the preceding section, we added five star-symbol instances to the main timeline of sky.fla. As a result, at runtime, those five instances become display object children of sky.fla's document-class instance. To prove it, let's create a document class for sky.fla, and use getChildAt( ) to list the children of the main timeline. Here's the code:

package {
  import flash.display.MovieClip;

  public class Sky extends MovieClip {
    public function Sky () {
      for (var i:int=0; i < numChildren; i++) {
        trace("Found child: " + getChildAt(i));
      }
    }
  }
}

With the preceding document class in place, when sky.swf is exported and played in the Flash authoring tool's Test Movie mode, the following output appears in the Output panel:

Found child: [object Star]
Found child: [object Star]
Found child: [object Star]
Found child: [object Star]
Found child: [object Star]

Tip

Code in a .swf file's document class constructor method can access all manually placed child assets on the .swf file's first frame but not on the second or subsequent frames (because assets on a subsequent frame are not added as display children until the playhead reaches that frame).

From an ActionScript perspective, the five star-symbol instances on the main timeline are instances of the Star class, which inherits from ...

Get Essential ActionScript 3.0 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.