Duplicating MovieClips?

Hi all.

Is there a way to duplicate a MovieClip in AS3???

I’ve tried all methods I found (in google) but weirdly none works!!!

I am very pissed as people keep saying it works but it don’t!!!

Here goes my code testing 3 methods. just copy-paste it in frame1 and create a movieclip called “square”.

This is a test of candidates for implementing duplicateMovieClip.
The tests are based from solutions posted throught many forums.

Here is a resume of each candidate:

candidate1- copy all MovieClip properties from the target to the copy
candidate2- Use byteArray to copy the MovieClip at byte level
candidate3- Use ObjectUtil from flex library to create a Object clone.

import flash.utils.ByteArray;
import mx.utils.ObjectUtil;

function duplicateCandidate1(target:MovieClip):MovieClip {
        // create duplicate
        var targetClass:Class = Object(target).constructor;
        var duplicate:MovieClip = 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;
        return duplicate;

function duplicateCandidate2(target:MovieClip):MovieClip{
    trace("candidate 2 target: "+target);
    var bytes:ByteArray = new ByteArray(); 
    bytes.position = 0; 
    var clone:MovieClip=bytes.readObject();
    trace("candidate 2 clone: "+clone);
    return clone;

function duplicateCandidate3(target:MovieClip):MovieClip{
    var clone:MovieClip = ObjectUtil.cloneObject(target) as MovieClip;
    return clone;

var squarec1:MovieClip=duplicateCandidate1(square1);
var squarec2:MovieClip=duplicateCandidate2(square1); // return null
//var squarec3:MovieClip=duplicateCandidate3(square1); //commented as it return ReferenceError (ObjectUtil not defined).


candidate 2 fail, as it returns null!
candidate 3 fail, as ObjectUtil is not found in package mx.utils!
candidate 1 fail, as it don't copy any graphic content!

Can anyone solve it?

Any question just ask