Flash AS3 Function: Duplicate MovieClip Objects

Comments 4 Standard

Recently I’ve found that I need to duplicate movie clip objects. I’m not talking about creating a new instance of the class, but actually duplicating the object with transformations (ie width, height, rotation) and any other filters applied to it so that it’s an exact copy of another movie clip. This nifty little function does the trick and it will also include any scale9grid properties you have on the movie clip.

private function duplicateObject(source:MovieClip):MovieClip
{
	// create duplicate
	var sourceClass:Class = Object(source).constructor;
	var duplicate:MovieClip = new sourceClass();

	// duplicate properties
	duplicate.transform = source.transform;
	duplicate.filters = source.filters;
	duplicate.cacheAsBitmap = source.cacheAsBitmap;
	duplicate.opaqueBackground = source.opaqueBackground;

	if (source.scale9Grid) {
		var rect:Rectangle = source.scale9Grid;
		duplicate.scale9Grid = rect;
	}

	return duplicate;
}
Advertisements

4 thoughts on “Flash AS3 Function: Duplicate MovieClip Objects

  1. when i try to call like this …..duplicateObject(duplicateMovieClipName)…….or addChild(duplicateObject(duplicateMovieClipName))…..the duplicate object cant show?where it will place….??????

  2. When you duplicate the object it should be copying the x,y of the object you give it. That means if you add it to the stage without changing the x,y values it will be sitting directly on top of the object you just duplicated. Try changing the x,y values of the duplicated object and you should see it no problem.

  3. I’m trying to set this up to work when a button is clicked, but I can’t seem to get it to do anything. Do I need to plug in the object name somewhere in the code? or will it work as is?

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