views:

1046

answers:

3

Hi,

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?

Thank you for helping me out.

A: 

table.addChild(swf.getChildAt(i));

I think the problem is with this line since it will only copy a reference of the child.

kahoon
A: 

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.

fenomas
None of these solutions can help me out, because my swf doesn't have an export to AS and I don't want to use the BitmapData because I need to scale my movieClip with no loss in quality. So I created a new movieClip and added the other one as a child of it, not perfect, but good enough since like you said, there seems to be no other way to duplicate a movieClip in AS3. Thank you for the answer.
didibus
Well, if you don't actually need two copies of the clip - you just need it moved from one place to another - then reparenting it should work fine. (This is what your original code was doing, by the way - when you addChild() a clip somewhere, it is removed from where it was previously.) But as a side note, the Bitmap solution would still work with scaling. When you render a clip into a bitmap (with the BitmapData.draw() method), you can specify a transformation (with rotation, scaling, etc) to apply as the clip is rendered.
fenomas
A: 

Since it looks like you are loading a SWF you do have one other option...

Load the SWF into a ByteArray using URLoader rather than loader. Then you can can create Loader objects everytime you need a new copy of your SWF - you just use the loadBytes method of Loader to pull the SWF out of the ByteArray.

Branden Hall