Targeting dynamic instances and loaded content

You can also create an object using ActionScript and target it using a target path afterwards. For example, you can use the following ActionScript to create a movie clip. Then you can change the rotation of that movie clip using ActionScript, as shown in the next example:

To target a dynamically created movie clip instance:

  1. Create a new Flash document and save the file as targetClip.fla.
  2. Insert a new layer and rename the layer actions.
  3. Add the following ActionScript to Frame 1 of the actions layer:
    this.createEmptyMovieClip("rotateClip", this.getNextHighestDepth());
    trace(rotateClip);
    rotateClip._rotation = 50;
    
  4. Select Control > Test Movie to test your document.

    You can tell that you created a movie clip because of the trace statement, but you cannot see anything on the Stage. Even though you added code that creates a movie clip instance, you won't see anything on the Stage unless you add something to the movie clip. For example, you might load an image into the movie clip.

  5. Return to the authoring environment, and open the Actions panel.
  6. Type the following ActionScript after the code you added in step 3:
    rotateClip.loadMovie("http://www.helpexamples.com/flash/images/image1.jpg");
    

    This code loads an image into the rotateClip movie clip that you created with code. You're targeting the rotateClip instance with ActionScript.

  7. Select Control > Test Movie to test your document.

    Now you should see an image on the Stage that rotates 50º clockwise.

You can also target or identify parts of SWF files that you load into a base SWF file.

To identify a loaded SWF file:

TIP

It's generally a good idea to avoid using levels if you can load content into movie clips at different depths instead. Using the MovieClip.getNextHighestDepth() method enables you to create new movie clip instances on the Stage dynamically without having to check whether there is already an instance at a particular depth.

Setting variables using a path

You can set variables for instances that you nest inside of other instances. For example, if you want to set a variable for a form that's inside another form, you can use the following code. The instance submitBtn is inside of formClip on the main timeline:

this.formClip.submitBtn.mouseOver = true;

You can express a method or property of a particular object (such as a movie clip or text field) using this pattern. For example, the property of an object would be

myClip._alpha = 50;