R – How to copy the content of one movie clip (with many frames) to another movie clip in flash actionscript 3

actionscript-3flash

I need a way to copy the content of one movie clip (with many frames) into another dynamically using as3.

This is how I tried to do it:

var table:Table = new Table(); //A custom class that extends MovieClip.
var swf:MovieClip = this.content.swf; //The movie clip I want to copy from.
for (var z:int = 0; z < swf.totalFrames; z++) //for every given frame of swf.
{
   swf.gotoAndStop(z);
   table.gotoAndStop(z);

   for (var i:int = 0; i < swf.numChildren; i++) //for every child of swf.
   {
      table.addChild(swf.getChildAt(i)); //copy child from swf to table.
   }
}

This does not work appropriately. It only copies the first frame. How can I copy every frame of swf onto table so that table can play and stop through the animation just as swf can do?

Best Answer

There are several reasons why this won't work, but the essential one is that frames don't exist at runtime the way you're trying to access them. Frames are an author-time concept - when you put certain contents on frame 1 and different contents on frame 2, at runtime Flash does not keep track of different sets of children for each frame. What's compiled into the SWF is merely instructions that certain children should be automatically added or removed when traversing from frame 1 to 2, or vice versa.

If you simply want a second copy of a given MC, that plays through the same animations as the first, what you rather need to do is to duplicate the object itself. In AS2 you could have done with with the duplicateMovieClip() API, but unfortunately AS3 has no equivalent command. Instead, you have to create a new instance of the clip you want to duplicate, with code of this form:

var swf:MovieClip = this.content.swf;
var swfClass:Class = swf.constructor;
var newSwf:MovieClip = new swf.constructor;

Note that this will not work unless the object you're trying to duplicate (this.content.swf) was attached to a class (i.e. the "export for ActionScript" check box was selected) when it was published. If it wasn't, then so far as I know there is no way to duplicate it in AS3. If this is the case, I think the best you can do is a workaround - for example, instead of creating a duplicate of the clip, you could create a Bitmap, and every frame simply render the appearance of the original clip into a BitmapData, and paint that data into the "duplicate" Bitmap.

Related Topic