View comments | RSS feed

duplicateMovieClip()

Availability

Flash Player 4.

Usage

duplicateMovieClip(target:String, newname:String, depth:Number) : Void

Parameters

target The target path of the movie clip to duplicate.

newname A unique identifier for the duplicated movie clip.

depth A unique depth level for the duplicated movie clip. The depth level is a stacking order for duplicated movie clips. This stacking order is similar to the stacking order of layers in the Timeline; movie clips with a lower depth level are hidden under clips with a higher stacking order. You must assign each duplicated movie clip a unique depth level to prevent it from replacing SWF files on occupied depths.

Returns

Nothing

Description

Function; creates an instance of a movie clip while the SWF file is playing. The playhead in duplicate movie clips always starts at Frame 1, regardless of where the playhead is in the original movie clip. Variables in the original movie clip are not copied into the duplicate movie clip. Use the removeMovieClip() function or method to delete a movie clip instance created with duplicateMovieClip().

Example

In the following example, a new movie clip instance is created called img_mc. An image is loaded into the movie clip, and then the img_mc clip is duplicated. The duplicated clip is called newImg_mc, and this new clip is moved on the Stage so it does not overlap the original clip, and the same image is loaded into the second clip.

this.createEmptyMovieClip("img_mc", this.getNextHighestDepth());
img_mc.loadMovie("http://www.macromedia.com/images/shared/product_boxes/112x112/box_studio_112x112.jpg");
duplicateMovieClip(img_mc, "newImg_mc", this.getNextHighestDepth());
newImg_mc._x = 200;
newImg_mc.loadMovie("http://www.macromedia.com/images/shared/product_boxes/112x112/box_studio_112x112.jpg");

To remove the duplicate movie clip, you could add this code for a button called myButton_btn.

this.myButton_btn.onRelease = function(){
   removeMovieClip(newImg_mc);
};

See also

MovieClip.duplicateMovieClip(), removeMovieClip(), MovieClip.removeMovieClip()

Comments


spiffier said on Sep 16, 2004 at 7:07 AM :
so how do you create duplicate that does include all attrubutes?
If duplicateMovieClip() doesn't and neither does mc1 = mc2 then what does?
decswxaqz said on Sep 22, 2004 at 7:28 AM :
Maybe if you declared the variables as globals and then referenced them from your movie scripts as "_global.variable_name". If you wanted to change them for all movies you would have to use "_global.varibale_name = " otherwise you would only be altering a local copy of the variable. I think that's how it works
No screen name said on Oct 12, 2004 at 1:43 PM :
What is the point of having an example that frankly is a stupid example?

If the function name "duplicateMovieClip()" actually DUPLICATED the
movie clip (and the contents thereof!), then we wouldn't be having this
discussion.

ie. Notice how you have to use LoadMovie() TWICE!!! Duh!!!
No screen name said on Oct 21, 2004 at 3:28 PM :
This is very frustrating. The duplicateMovieClip is the exact same
function as createEmptyMovieClip. The duplicate loadMovie command
is just laughable.

Would someone please address this problem? What is the attribute of
the actual contents of the movieClip's image?
Gratman said on Nov 23, 2004 at 9:47 AM :
i think they are demonstrating that a loaded movie does not get duplicated, only the actual movie that is duplicated. Which does kinda suck.

Also it is not the same as createEmptyMovieClip unless the movie duplicated is empty. if you have a drawing or etc. in it it will be duplicated.

This is all old flash 5 stuff anyway. I just create symbols and use attachMovieClip now.
No screen name said on Dec 1, 2004 at 6:54 AM :
The only thing to remember is :
A movieclip from which content has been loaded via loadMovie isn't duplicable with the duplicateMovieClip() function.
The behavior is :
Only the shell (is reference) of the original movieclip is duplicated, not is content.
No screen name said on Jan 18, 2005 at 5:14 AM :
Is the example working? Do you think it needs event handler before it can work? Could anyone point me to the right direction on doing duplicateMovieClip. It is better to have a working example.
Neeil said on Jan 19, 2005 at 4:14 AM :
So we can't duplicate a loaded clip - seems silly - but, does anyone know if a loaded jpg would be cached so that subsequent loads would be quicker?
Francis Cheng said on Jan 19, 2005 at 7:41 AM :
The example does work, and an event handler is not necessary to run this example code. However, if you want the code to execute after the user clicks a button that you create, then you will need to implement some kind of event handler.
Neeil said on Jan 19, 2005 at 9:21 AM :
In answer to my question - loading the same jpeg multiple times IS quick after the first load because the image is cached locally. So, it's not all bad.
zasdzasdzasd said on Feb 11, 2005 at 9:41 PM :
so is there a workaround to this? i don't want to rely on browser cache, some people don't have it, i want to explicitly cache the loaded movie, and duplicate it if i want. anybody found a way?
thanks
IntensityLevel said on Feb 17, 2005 at 11:34 AM :
As far as I know, there is no way to explicitly cache the loaded movie, and duplicate it. Which is the single most annoying thing about using flash, imo. If I have a photogallery with thumbnails, some users must download all the fullsize images twice during preloading because of my thumbs (which are normally duplicated from the high res and scaled down).

Someone mentioned using symbols and attachMovie... is there a way to dynamically load an image into a symbol and reuse it multiple times?
No screen name said on Apr 1, 2005 at 2:18 AM :
I really hope this issue is addressed soon. I am working on a project that loads a SWF file specified in an external XML file, and then needs to duplicate it about 50 times (it's a particle).
The project requires that the SWF file is seperate and specified in XML, so I can't just put it inside the main SWFs library.
It is really irritating to have to load it 50 times - I would like to load it once, then duplicate it 49 times.
No screen name said on Apr 8, 2005 at 9:16 AM :
To duplicate attributes (not loaded images) see MovieClip.duplicateMovieClip() which can initialise the new clip from an initObject.

If you put your image url into an attribute of the original clip, and use the original clip as the initObject, you will then have the url easily available to reload the image (hopefully from browser cache).

something like...

_root.item.imgurl = 'blah.jpg';
_root.item.img.loadMovie(this.imgurl); // first load of image

// later...
_root.item.duplicateMovieClip("copy"+_root.num_items, _root.num_items,_root.item);

// & on _root.item
onClipEvent (load) {
this.createEmptyMovieClip('img', 1);
if (this.imgurl <> undefined) {
this.img.loadMovie(this.imgurl);
}
}
yaroukh said on Apr 16, 2005 at 2:53 PM :
> Usage
> duplicateMovieClip(target:String, newname:String, depth:Number) : Void

Doc says first parameter should be String, but in the example you pass a MovieClip as the first parameter.
Did I get something wrong?

Furthermore I don't quite get why this function doesn't return the newly created MovieClip instance.
Francis Cheng said on Apr 16, 2005 at 3:48 PM :
Hi yaroukh,

Thanks for pointing that out. There should be a second line in the Usage section that shows that the parameter can also be a movie clip. I'll change the document source file to reflect that. As for your second question, there are actually two duplicateMovieClip() functions. More specifically, the one on this page is a global function, and the one that's a member of the MovieClip class is a method. The global function version has been around since Flash 4, which is why it returns void (we left it in for backward compatibility and put the changes you seek into the new MovieClip method). The MovieClip class version returns a reference to the duplicated movie clip. The Movieclip version also takes an additional parameter, called initObject, which you can read about in that method's entry, a link to which is in the See also section above.
cmoffitt said on Jun 29, 2005 at 5:24 PM :
OK, back to the original problem.
Using the duplicateMovieClip() method fails to duplicate the clip contents after a loadMovie() method has been called on the original clip.
This sucks Macromedia.
Sure, the proposed duplicate could just call loadMovie() with the same URL, but the cache (from disc) is slow enough to cause a noticeable delay.
Is there a decent workaround for this?
No screen name said on Jul 16, 2005 at 3:52 PM :
Okay, a lot of people are having problems with duplicateMovieClip on a clip in which you loaded another movie.
First, I don't think this beheviour is wrong, because it duplicates the movie in it's initial state, which is empty when you created it.
But - believe it or not - I found a work-around! It's a bit complex but it works.
Instead of duplicating the movieclip you created and loaded the movie into, you duplicate a movieclip that is inside that movieclip. So the swf file you load should have all it's contents in a single movieclip, and you duplicate that.
So you kind of load a container for your duplicateable movieclip, which has one instance created already, and you duplicate that instance. It will still be inside the container-movieclip, so it's coordinates are relative to that, but if you put the container on 0,0 it shouldn't be a problem.

Yes, you can thank me :)

Or if you have any questions about this method, feel free to contact me (sven [-at-] ontspoord [-dot-] com
No screen name said on Jul 16, 2005 at 3:55 PM :
Ow and that only works with swfs, not jpegs, but I suppose you could make an swf which has the jpeg as a movieclip and it should work.
TSC Mike said on Jul 26, 2005 at 7:53 AM :
I can't understand why when I do an onClipEvent(load) { duplicateMovieClip(_parent.clipTobeduplicated, _parent.newClip, anylevel) } !
When I trace what's happening, I see that for some reason the onClipEvent(load) doesn't run ONCE,... it runs insanely over and over, and it tries duplicating a MC rapidly so much that it causes flash to give me the "script is making the program slow" warning popup.

Why?
No screen name said on Jul 26, 2005 at 4:19 PM :
TSC Mike:

you probably don't have stop(); on the mc the onClipEvent(load); is on. It is looping over and over doing loadMovie every time.
scottPadgett said on Jul 27, 2005 at 9:37 AM :
Does anyone know if the initObject is really supported by the Player 6
and later, or if it might just be 7 and later??

I've got a movie that won't run in the 6 player, and the only thing in
debate with regard to version compatability would be this initObject
parameter in the duplicateMovieClip() method...

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/mx2004/main_7_2/00001336.html