Working with Movie Clips and Buttons > Using actions and methods to control movie clips > Dynamically adding a movie clip or sound to the Stage

 

Dynamically adding a movie clip or sound to the Stage

To retrieve a copy of a movie clip or sound from the library and play it as part of your movie, you use the attachMovie method of the MovieClip object or the attachSound method of the Sound object. The attachMovie method loads a movie clip as a child of the clip that loads it and plays it as the movie runs. The attachSound method attaches a sound to an instance of the Sound object.

To use ActionScript to attach a movie clip or sound from the library, you must assign a unique linkage identifier to the movie clip or sound. You can assign this name in the Linkage Properties dialog box.

When a movie plays, Flash loads all movie clips and sounds that are added with attachMovie or attachSound before the first frame of the movie. This can create a delay before the first frame plays. When you assign a linkage identifier to an element, you can also specify whether this content should be added before the first frame. If it isn't added in the first frame, you must include an instance of it in some other frame of the movie; if you don't, the element will not be exported to the SWF file.

 
To name a movie clip:

1

Choose Window > Library to open the Library panel.

2

Select a movie clip in the Library panel.

3

In the Library panel, choose Linkage from the Library panel options menu.

The Linkage Properties dialog box appears.

4

For Linkage, select Export for ActionScript.

5

For Identifier, enter an ID for the movie clip.

6

If you don't want the movie clip or sound to load before the first frame, deselect the Export in First Frame option.

7

Click OK.

 
To attach a movie clip to another movie clip:

1

With the Actions panel open, select a frame in the Timeline.

2

In the Actions toolbox (at the left of the Actions panel), click the Objects category, the Movie category, and the MovieClip category, and double-click the attachMovie method.

3

For the object parameter, enter the instance name of a movie clip on the Stage.

4

Enter values for the following parameters:

For idName, specify the identifier you entered in the Linkage Properties dialog box.

For newName, enter an instance name for the attached clip so that you will be able to target it.

For depth, enter the level at which the duplicate movie will be attached to the movie clip. Each attached movie has its own stacking order, with level 0 as the level of the originating movie. Attached movie clips are always on top of the original movie clip. Here is an example:

myMovieClip.attachMovie("calif", "california", 10);