Recipe 11.17. Adding Movie Clips at RuntimeProblemYou want to be able to add movie clips to your movie at runtime. SolutionSet the movie clip symbol to export, and use attachMovie( ) to add the instance to the movie. DiscussionYou 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:
The following creates a new movie clip instance named mCircle. The instance is based on the symbol in the library with a linkage identifier of Circle. With the assumption that you place the code on a keyframe on the main timeline, mCircle will be created within the main timeline. this.attachMovie("Circle", "mCircle", 1); The preceding code creates mCircle with a depth of 1. You can use the getNextHighestDepth( ) method to get a valid depth without having to hardcode a value. this.attachMovie("Circle", "mCircle", this.getNextHighestDepth()); You can read more about depths in Recipe 11.9. You can read more about the getNextHighestDepth( ) method in Recipe 11.10. Most of the concepts that apply to duplicating movie clips also apply to attaching movie clips. Refer to Recipe 11.9 for more details. |
Tripod >> 3pod Tips & Learning and manuals for educations