Adding parameters to dynamically created movie clips

When you use MovieClip.attachMovie() and MovieClip.duplicateMovie() to create or duplicate a movie clip dynamically, you can populate the movie clip with parameters from another object. The initObject parameter of attachMovie() and duplicateMovie() allows dynamically created movie clips to receive clip parameters.

For more information, see attachMovie (MovieClip.attachMovie method) and duplicateMovieClip (MovieClip.duplicateMovieClip method) in the ActionScript 2.0 Language Reference.

To populate a dynamically created movie clip with parameters from a specified object:

Do one of the following:

The initObject parameter specifies the name of the object whose parameters you want to use to populate the dynamically created movie clip.

To populate a movie clip with parameters by using attachMovie():

  1. In a new Flash document, create a movie clip symbol by selecting Insert > New Symbol.
  2. Type dynamic_mc in the Symbol Name text box, and select the Movie Clip behavior.
  3. Inside the symbol, create a dynamic text field on the Stage with an instance name of name_txt.

    Make sure this text field is below and to the right of the registration point.

  4. Select Frame 1 of the movie clip's Timeline, and open the Actions panel (Window > Actions).
  5. Create a new variable called name_str, and assign its value to the text property of name_txt, as shown in the following example:
    var name_str:String;
    name_txt.text = name_str;
    
  6. Select Edit > Edit Document to return to the main Timeline.
  7. Select the movie clip symbol in the library, and select Linkage from the Library pop-up menu.

    The Linkage Properties dialog box appears.

  8. Select the Export for ActionScript option, and Export in first frame.
  9. Type dynamic_id into the Indentifier text box, and click OK.
  10. Select the first frame of the main Timeline, and add the following code to the Actions panel's Script pane:
    /* Attaches a new movie clip and moves it to an x and y coordinate of 50 */
    this.attachMovie("dynamic_id", "newClip_mc", 99, {name_str:"Erick", _x:50, _y:50});
    
  11. Test the Flash document (Control > Test Movie).

    The name you specified in the attachMovie() call appears inside the new movie clip's text field.

For a sample source file, gallery_tween.fla, that provides an example of how to use ActionScript to control movie clips dynamically while loading image files into a SWF file, which includes making each movie clip draggable, see the Flash Samples page at www.adobe.com/go/learn_fl_samples. Download and decompress the Samples zip file and navigate to the ActionScript2.0/Galleries folder to access the sample.

For a sample source file, animation.fla, that creates and removes numerous movie clips at runtime, see the Flash Samples page at www.adobe.com/go/learn_fl_samples. Download and decompress the Samples zip file and navigate to the ActionScript2.0/Animation folder to access the sample.