ActionScript.org Flash, Flex and ActionScript Resources - http://www.actionscript.org/resources
Using duplicateMovieClip
http://www.actionscript.org/resources/articles/39/1/Using-duplicateMovieClip/Page1.html
Evgueni Strok
This user is yet to take control of their account and provide a biography. If you are the author of this article, please contact us via support AT actionscript DOT org. 
By Evgueni Strok
Published on September 9, 2005
 
Written by: Evgueni Strok [email:strok@actionscript.org]
Difficulty Level: Beginner
Requirements: Flash 5 or higher

Page 1 of 1
Written by: Evgueni Strok [email:strok@actionscript.org]
Difficulty Level: Beginner
Requirements: Flash 5 or higher
duplicatemovieclip creates a copy of a movie clip specified using an instance name.

I think the best way to understand how it works is to start from a very basic example and step by step we'll try to add some new lines of code.

EXAMPLE 1

To duplicatemovieclip we need a Movie Clip (MC) which will be duplicated.
In my example I made a MC_1 with Instance name "box"



I'll call a duplicatemovieclip function by using button with this script on it:
[as]on (release) {
duplicatemovieclip ("box", "box2", 1);
}[/as]

box -- Instance name of MC which is suppose to be duplicated
box2 -- name for duplicated MC


Download FLA

The duplicated MC inherits the _x and _y coordinates of the clip it is copied from, so it will appear above the original but we can move this using a setProperty action. See example below.

EXAMPLE 2


Download FLA

[as]on (release) {
duplicatemovieclip("box", "box2", 1);
setProperty ("box2", _x, "200");
}
[/as]

This same code could also be written as:

[as]on (release) {
_root.box.duplicatemovieclip("box2", 1);
box2._x=200;
}[/as]

EXAMPLE 3

This example duplicates an MC 20 times using a while loop. See the Scripted Loops tutorial for more on loops, and the Advanced Pathing tutorial for more on the strange paths used in this example.


Download FLA

[as]on (release) {
count = 1;
while (count<20) {
_root.box.duplicatemovieclip("boxx"+count, count);
count += 1;
}
}[/as]

The next example duplicates an MC 20 times and uses "random" properties for each duplicated MC. Thus each new MC will get a random _x, _y, _xscale, _xscale positions and _alpha properly.

EXAMPLE 4


Download FLA

[as]on (release) {
count = 1;
while (count<20) {
_root.box.duplicatemovieclip("boxx"+count, count);
_root["boxx"+count]._x = random(550);
_root["boxx"+count]._y = random(150);
_root["boxx"+count]._xscale = random(150);
_root["boxx"+count]._yscale = random(150);
_root["boxx"+count]._alpha = random(100);
count += 1;
}
}[/as]
Note: If you reuse the same Instance name for duplicated MC you'll replace the old one. The above code creates a new unique instance name each time using a counter.

EXAMPLE 5

This example has the same actionscript which has been added on frame.
And a MC has a small timeline.


Download FLA