Duplicate Movie Clip Action Script 3

After long 2 years of Action Script 2 Development I have shifted my Self to Action Script 3. Initially a great experience, Still is but when i came across DuplicateMovieClip function, I was shocked that there was no direct way of doing it in AS3. After doing my research on the internet and studying various Solutions, I found one that works normally best except one problem. I have discussed the problem at the end of this article. Any ways Duplicate Movie Clip in flash action script 3 has limitations. There should have been a Clone Method in DisplayObject Class. Here is the required Action Script Code

Code:
<pre>
 package com.senocular.display {

    import flash.display.DisplayObject;
    import flash.geom.Rectangle;

    /**
     * duplicateDisplayObject
     * creates a duplicate of the DisplayObject passed.
     * similar to duplicateMovieClip in AVM1
     * @param target the display object to duplicate
     * @param autoAdd if true, adds the duplicate to the display list
     * in which target was located
     * @return a duplicate instance of target
     */
    public function duplicateDisplayObject(target:DisplayObject,</pre>
<pre>
	autoAdd:Boolean = false):DisplayObject {</pre>
<pre>
	// create duplicate
        var targetClass:Class = Object(target).constructor;
        var duplicate:DisplayObject = new targetClass();

        // duplicate properties
        duplicate.transform = target.transform;
        duplicate.filters = target.filters;
        duplicate.cacheAsBitmap = target.cacheAsBitmap;
        duplicate.opaqueBackground = target.opaqueBackground;
        if (target.scale9Grid) {
            var rect:Rectangle = target.scale9Grid;
            // Flash 9 bug where returned scale9Grid is 20x larger than assigned
            rect.x /= 20, rect.y /= 20, rect.width /= 20, rect.height /= 20;
            duplicate.scale9Grid = rect;
        }

        // add to target parent's display list
        // if autoAdd was provided as true
        if (autoAdd && target.parent) {
            target.parent.addChild(duplicate);
        }
        return duplicate;
    }
}</pre>

Place the above code in an ActionScript code file named "duplicateDisplayObject.as" Now to use the Above code in your fla file write the following AS3 code

Code:
<pre>
import com.senocular.display.duplicateDisplayObject;</pre>

btnDuplicate.addEventListener(MouseEvent.CLICK, duplicateClickHandler);</pre>

function duplicateClickHandler(evt:MouseEvent)</pre>

{
    var newInstance:DisplayObject = duplicateDisplayObject(mcBg, true);</pre>
    newInstance.x += 200; // shift to see duplicate</pre>

}</pre>

Problem that i saw while duplicating MovieClip in Action Script 3 using the following solution was strange that it was unable to Duplicate MovieClip which had no named instance of any other MovieClip added as a child. A Bug i guess. Thus i was unable to Duplicate a dynamically Loaded MovieClip. It only left me with option to load the dynamic movie clip every time from server or hard drive which made the duplicating process slower a bit. But still i love Action Script 3. Hopefully in the next release there will be a better way in DisplayObject class to Duplicate MovieClip in ActionScript 3.

Kindly Let me know about your remarks if this article is being helpful in solving your problem.

Download Sample Project : duplicate-movieclip-actionscript-3-best-sample.zip

A running sample can be viewed at the following link

www.imaginationdev.com/actionscript-blog/samples/duplicate/DuplicateMovie.swf Solution i found is based on Senocular’s post on the following forum. So Thanks to Senocular for sharing these ideas as well. http://www.kirupa.com/forum/showthread.php?p=1939827

Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

Comments

I was wondering if there was a type of “duplicateChild” command in AS3. This is what I was looking for, I tested it and it worked perfectly!

Thank you very much for sharing!

[Reply]

can only get it to duplicate ‘mcBg’. there’s an movie clip in the library which is called ‘temp’; it is visually identical to mcBg. However if I drag an instance of ‘temp’ onto the stage and give it an instance name of ‘temp’, substituting ‘temp’ for ‘mcBg’ in the following lines does not make ‘temp’ duplicate itself:
mcBg.addEventListener(MouseEvent.CLICK, duplicateClickHandler);
function duplicateClickHandler(evt:MouseEvent)
{
var newInstance:DisplayObject = duplicateDisplayObject(mcBg, true);

works perfect with mcBg. I have also tried imports other objects and converting them to movie clips with no success

[Reply]

hi , you need to create a named instance … make any dummmy object in the temp mc and name it … Than copy the object it will work.. This problem happened with me as well .. It took me time to figure it out. Also its kind of a Bug in this approach. as its not possible to duplicate dynamic objects..

To duplicate dynamic objects what you need to do is to create a class for that clip and associate the clip to that class.

create an object of that class and add it on the stage or what eer movie clip you want to .. this is the best way to do it

Thank you

[Reply]

Hi,

This is good, but if I want to duplicate an object of some Sprite or MovieClip and in that object I may have added some more TextFields or other objects, then how can I duplicate that object.

-Naresh

[Reply]

admin Reply:

HI, i have posted an updated code for duplicating objects in flash action script.

http://actionscript-blog.imaginationdev.com/25/duplicate-movieclip-flash-action-script-enhanced/

Thanks

[Reply]

you are merely creating a new instance of a class… duplication implies preserving state of the source.

[Reply]

What you need to do is associate that sprite or the movie clip to a class and than create new instance of that class and it will do all that you need .. it will duplicate every thing with in that movieclip

if you need more help than kindly let me know

Thank you

[Reply]

Interesting, I haven’t heard about that. Could you point me to a class, method or article regarding this?

[Reply]

please check this URL — http://www.s d p l a b s.com/project_/briarcha/

i am using this duplicate code in this small application .. open the link and please click on any number in the map … then a pop up window appears with the dummy details of that lot .. and each detail row is a duplicate … everything works fine on windows but it does not show any of the duplicate on MAC. The duplicates take the height but not visible. Thanks in advance for any help.

Varun

[Reply]

i dont understand what is the need of duplicating each row

cause the feilds are fixed for each click . why you need to creat dynamic feilds ?

secondly .. please explain a bit more or send me the code to be more specific

Thank you

will try my best to sort things for you

Billy

[Reply]

This is the best ref..
however I need dynamic movieclip duplication, can you tell me how to do this?

To duplicate dynamic objects what you need to do is to create a class for that clip and associate the clip to that class.

create an object of that class and add it on the stage or what eer movie clip you want to .. this is the best way to do it

Million Thank in advance.

[Reply]

To duplicate dynamic objects what you need to do is to create a class for that clip and associate the clip to that class.

create an object of that class and add it on the stage or what eer movie clip you want to .. this is the best way to do it

Well i will right up an article soon on this just to demonstrate how this works

… Thank you

Bilal

[Reply]

Hi guys, this script doesn’t work in FLASH CS4, anyone knows the reason for this? THanks alot!

[Reply]

Haven not tried flash cs4 till now … thanx

[Reply]

Does this works or not with CS4 ?

thank You

[Reply]

Same problem in here with FLASH CS4.

[Reply]

Hi, it does work on all players. But I have not worked on with flash CS4 , that why i am not that sure about that.

Thanks

[Reply]

well i was using the new post the admin poseted about duplicating dynamic clips via classes, and that i can confirm works in cs4.

[Reply]

admin Reply:

Yah you are right . the new post does work.
http://actionscript-blog.imaginationdev.com/25/duplicate-movieclip-flash-action-script-enhanced/

[Reply]

This is real bad !! whats all this package class junk ??

All i want to do is duplicate a movie clip. And what happens when you publish it to the web, doesa it need all the *.AS files to go with it ?

Sorry but AS3 just takes the buscuit !

heres my game so far, without package class junk.

http://www.pinkpig.weebly.com

Andy.

[Reply]

[...] tells to the best possible solution so no Doubts.here A better explanation to this method is given here Hope it helps [...]

Leave a comment

(required)

(required)