11.17. Adding Movie Clips at Runtime

Problem

You want to be able to add movie clips to your movie at runtime.

Solution

Set the movie clip symbol to export, and use attachMovie( ) to add the instance to the movie.

Discussion

You can add movie clips to your Flash movie at runtime using ActionScript. Previously, you read about how to use duplicateMovieClip( ) to create duplicates of an existing movie clip. However, you can actually add new instances to the stage using ActionScript without even having any prior instances that you created during authoring time. This is a really powerful technique, because it allows you to construct your Flash movies almost entirely with ActionScript.

In order to add movie clip instances to the stage at runtime without any previous instances, you need to do two things:

  1. Export the movie clip symbol. Flash does not include all symbols from your library in the exported SWF. In order to ensure the smallest file size, Flash exports only those symbols that are used in the movie. Therefore, if you don’t create any authoring time instances of a movie clip symbol, Flash will not export it in the final SWF unless you tell it to do so. You can tell it to do this by using the Linkage settings from the library:

    1. Open the library by choosing Window → Library or by pressing Ctrl-L.

    2. Select the movie clip symbol in the library, and either right-click/Control-click the item in the symbol list, or open the Library menu and select the Linkage option.

    3. In the Linkage Properties dialog ...

Get Flash 8 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.