lang="en-US"> Flash AS3 Function: Duplicate MovieClip Objects –  Design1online.com, LLC

Flash AS3 Function: Duplicate MovieClip Objects

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;
}

Leave a Reply