11.9. Creating New Movie Clips Based on Existing Movie Clips

Problem

You want to create a duplicate movie clip instance based on an existing instance.

Solution

Use the duplicateMovieClip( ) method.

Discussion

With the duplicateMovieClip( ) method, you can quickly create duplicates of movie clip instances already on the stage. This method creates a copy of the movie clip instance from which it is invoked with a new instance name and depth:

	// Create a new movie clip named mNewInstance based on the movie clip named
	// originalInstance that already existed on the stage. The new movie clip is
	// created at depth 1.
	mOriginalInstance.duplicateMovieClip("mNewInstance", 1);

Tip

Make sure that you use a unique depth for the new movie clip. If you use a depth that corresponds to an existing instance, that existing instance will be overwritten.

Additionally, you can specify a third, optional parameter for the duplicateMovieClip( ) method. This parameter is known as the initialization object, and the properties and values of the initialization object are assigned to the new instance. The parameter value should be in the form of an ActionScript Object object, which you can create one of two ways:

  • Using the constructor and assigning properties and values via dot notation:

    	var oInitialization:Object = new Object();
    	oInitialization.property1 = "value1";
    	oInitialization. property2 = "value2;
  • Using the object literal notation:

    	var oInitialization:Object = { property1: "value1", property2: "value2"};

Both of ...

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.