Schemaphic Systems Blog

Sharing experiences in Flash, Flex and AIR

Duplicate MovieClip in AS3.0

with 4 comments

Like duplicateMovieClip in AS2.0, here is an API to do the same in AS3.0, So just chill 🙂

/**
* usage: var dupMC:MovieClip = duplicateDisplayObject( MC1 );
* @params -  target:DisplayObject - The movieclip to be duplicated.
*            autoAdd:Boolean - default is false, setting it to true will add the new
*            duplicate movieclip to the parent of the to be duplicated movieclip
*/

public function duplicateDisplayObject( target:DisplayObject, autoAdd:Boolean = false ):DisplayObject
{
     // create duplicate
     var targetClass:Class = Object(target).constructor;
     var duplicate:Sprite = 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;
          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;
}
Advertisements

Written by Deepanjan Das

February 1, 2011 at 8:58 AM

4 Responses

Subscribe to comments with RSS.

  1. Hi there,
    It seems like for this method to work, the duplicated DisplayObject has to have a linkage id in the library- set Export for Actionscript in the library item’s properties.

    goliatone

    May 12, 2011 at 9:43 PM

    • Not really, I have tested this with Flex SDK 3+ and it worked fine.
      Not sure if this will work with flash. Just need to check the APIs used compatibilities.

      Deepanjan Das

      May 17, 2011 at 10:20 PM

  2. I spent long time to duplicate movieClip. But no luck.
    This blog help me lot to complete my task. Thanks

    Stephen

    June 4, 2012 at 12:41 PM


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: