2

I have a movieclip that I need to duplicate dynamically based on an outside variable. e.g. clip1, clip2, etc This variable changes so I can't hardcode the number of times it occurs.

Is there a way to dynamically create this movieclip multiple times and align it according on the screen?

I wasn't able to find anything on Google..

Regards Luben

3 Answers 3

2

How about using a function that duplicates a DisplayObject. This function returns a new instance of the Class the provided object is:

function duplicateDisplayObject( dspObj:DisplayObject ):DisplayObject 
{
    var class_name:String = getQualifiedClassName( dspObj );
    var definition:Class = getDefinitionByName( class_name ) as Class;
    return new definition() as DisplayObject;
}

(MovieClip, Sprite, and Bitmap all inherit from DisplayObject)

Edit:

If you need to use it with MovieClips, you can simply:

var myDuplicate:MovieClip = duplicateDisplayObject( myOriginalMovieClip ) as MovieClip;
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Julian just what I was looking for - a note If I use DisplayObject I cannot use .gotoAndPlay(). Changing it to MovieClip solves the problems.
That's true, but you don't need to change the duplicateDisplayObject function. Simply cast the return value when you use it. See my example above.
Please note the above function does not work for movieClips within other movieClips. Any Ideas on how to duplicate such objects?
1
var clip:YourClip;

for(var i:uint=0;i<5;i++)
{
     clip = new YourClip();
     addChild(clip);
     clip.x = 20 + clip.width * i * 1.2;
     clip.y = 20;
}

Comments

0

You won't be able to just duplicate an instance of a clip. You can instantiate another instance of it's class though, and then adjust the position of it.

var clip1:MyClip = new MyClip();

var copies:int = 10;

for(var i:int = 0, i < copies; i++) {
    var clone:MyClip = new MyClip();
    clone.name = 'clip' + i
    clone.x = ....;
    clone.y = ....;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.