views:

1044

answers:

3

Hello,

I'm working on an Flex application which uses many objects, e.g. LEDs, bulbs, gauges created in Flash. The objects internally consist of several small MovieClips and have their own logic inside. One of the initial requirements was that the objects have to be loaded at runtime, thus they were exported as SWF, not SWC. However, we ran into problem when we tried to duplicate loaded SWF. It turned out that MovieClip class doesn't have neither copying constructor nor method that would allow us to clone existing MovieClip. This way we'd end up loading an object every time from hdd, which involves a lot of overhead. Is it possible that language like ActionScript 3 doesn't have such a mechanism? Have we missed something out? If not, am I correct that the only solution is to use Flash Component Kit, make some custom components and include them as SWC at compile time?

+1  A: 

After you load the MovieClip is it possible to use getDefinitionByName() as shown here?

http://livedocs.adobe.com/flex/3/langref/flash/utils/package.html#getDefinitionByName()

cliff.meyers
This is the right way to about it. Just remember to link your symbols to classes in Flash. That way you know what name to look up! ;)
Troy Gilbert
A: 

You are correct in that there is no built in way to duplicate a movieclip. There are however work arounds. The esiest way as I see it is to give the movieclips classes.

You don't have to make the actual classes. Just write any name in the class field when setting up linkage on the top most movieclip that needs to be copied. So a name for you LED movieclip, another name for the bulb etc.

The benifit is that now you have a class that you can initiate objects from.

No when you grap one of the movieclips you can duplicate it with the following method:

public function DuplicateDisplayObject(dO:DisplayObject):DisplayObject
{
    if(dO == null)
        return null;
    var dOClass:Class = Object(dO).contructor;
    return DisplayObject(new dOClass());
}

This assumes of cause that you can actually get a hold of one of the movieclips first. And mind you that it doesn't copy the state of the movieclip. Another more importent note is that this only works if the you link the movieclips to classes. The classes doesn't have to exist (flash will create empty classes for you).

Other solutions could be:

  • Compiling against the classes without including them (see the "external-library-path" tag for the flex compiler), and load the them at runtime (from swf).
  • Compiling against the classes as a RSL (Runtime Share Library) the swc will be loaded at runtime.

Adobe has more info on how to do that, should be easy to find on their website.

A more exotic solution would be copy the bytecode of an object. Not sure if that would work with something on the displaylist, properly not.

About the solution using getDefinitionByName(): If I remember correctly you still need to give the movieclips fake classes, since getQualifiedClassName only returns MovieClip class. But I could be wrong.

Lillemanden
A: 

Another solution:

        private function duplicateImg(sourceLoaderInfo:LoaderInfo, target:Image):void
    {
     var ba:ByteArray = sourceLoaderInfo.bytes;      
        var dupCliploader:Loader = new Loader();

        dupCliploader.contentLoaderInfo.addEventListener(
          Event.COMPLETE, bytesLoaded);

        dupCliploader.loadBytes(ba);
    }

    private function bytesLoaded(event:Event):void
    {   
 var mc:MovieClip = event.currentTarget.content as MovieClip;

 _img.source = mc;
 _img.width = mc.width;
 _img.height = mc.height+5;        
    }