简体   繁体   中英

AS3 load symbol from external swf library

I have 2 SWF, one of them (let's call it Resources.swf ), that contains several symbols (most of them MovieClips) on its library, but, none of them are added into the stage ( the timeline contains only one empty frame), and then, the other swf ( Main.swf ), where I need to import some of the symbols from the other SWF.

I have been looking around, and searching, but all the info that I saw, and tried, imports the symbols from the stage/timeline using things like:

loadedMC = MovieClip(loader.content);

or

loadedMC = MovieClip(event.target.content);

My symbols have a class definition, because, they are also used in other swf this way:

[Embed(source='assets/Resources.swf', symbol='SymbolName')]
public class Generic2 extends MovieClip

Is there any way to do this?

If not,do I have modify my Resources.swf to work this out or do I have other alternatives?

After loading the Resources.swf you can create instances of the loaded classes.

Here a snippet that may help:

var dynClass : Class = Class(getDefinitionByName("fully.qualified.ClassName"));
if(dynClass) 
{
    var app : Object = new dynClass();
    addChild(app as DisplayObject);
}

Follow these steps

  1. In flash, mark your class for export by bringing up "Properties" for the library item and checking "Export for ActionScript" and giving it a "Class" name.
  2. Publish Resources.SWF
  3. After loading Resources.SWF, you can create instances with this helper function
  4. You can then addChild() or whatever you want with the instance.

Helper Function

public function createInstance(mc:MovieClip, className:String, instName:String):MovieClip
{
    var cls:Class = mc.loaderInfo.applicationDomain.getDefinition(className) as Class;
    if (cls)
    {
        var instMC:MovieClip = new cls();
        instMC.name = instName;
        return instMC;
    }
    return null;
}

You need to have a as3 class, or just to export the symbol in the frame otherwise you can't reference it. Your swf will be empty if you don't export your movieclip... Basically you can just export it as a Certain class and then reference the empty class!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM