Icono
Icono

Reputation:

AS3 embed or import all classes in an external swf without referencing each class separately

I have an ArtLibrary.swf file which has hundreds of MovieClips exported with class names. I want to be able to use these movies in multiple different flash files i'm working on but I don't know how to properly reference them after using and embed command.

The following works:

[Embed(source="ArtLibrary.swf", symbol="BirdBodyColor_mc")]
var BirdBodyColor_mc:Class;

myMC:MovieClip = new BirdBodyColor_mc();
addChild(myMC);

In this example, I'm not sure how to reference the individual classes inside the "master class".

[Embed(source="ArtLibrary.swf")]
var MasterClass:Class;

myMC:MovieClip = new BirdBodyColor_mc();
addChild(myMC.BirdBodyColor_mc);

Upvotes: 6

Views: 18027

Answers (6)

Yep,

this answer comes 9 years too late but you can do it without actually creating a reference by using ApplicationDomain. So imagine that you have classes linked to your moveiclips in base_library.swf, which you are loading into your main swf. One movie clip has class linkage 'Slide2' In your main swf you have this

    private function loadSWF():void {
        var loader:Loader = new Loader();
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
        loader.load(new URLRequest("./lib/base_library.swf"));
    }

    private function onComplete(e:Event):void {
        trace(" ON COMPLETE LOAD ");
        var movie:* = LoaderInfo(e.currentTarget).content;

        // now try and create an instance of a slide
        var mc:MovieClip = createInstance(movie,"Slide2", "slide2");
        addChild(mc);
    }

    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;
    }

Upvotes: 0

Daniel
Daniel

Reputation: 629

Using FlashDevelop this is pretty simple to fix.

Right click your included swc from the Project list. Choose options then "include library (complete library)".

..you can now use getDefinitionByName to get a unreferenced class from your swc file.

Upvotes: 2

Cameron Yule
Cameron Yule

Reputation: 214

If you felt like getting fancy you could write a little script that would crack open a SWC (it's just a zip file), read the XML manifest that's inside and create a special "includer" class automatically. But as they say - I'll leave that as an exercise for the reader! ;-)

No need to do anything so complex (thankfully!). The include-libraries compiler argument will automatically include all classes from within your SWC, allowing the getDefinitionByName method to work as expected.

In your App-config.xml file (or alternatively passed directly as an argument to the compiler):

<include-libraries>
    <library>path/to/your.swc</library>
</include-libraries>

Then you're free to use getDefinitionByName as normal.

HTH.

Upvotes: 3

Icono
Icono

Reputation:

So I'm still stuck writing with a long, manually created as script that references each imported class. Granted the lines are much shorter than the embed method, one term even.

In the end, I made my ArtLibrary.fla and then just copied and pasted all the folders from that fla's library into each of my 5 other fla's. I didn't bother compiling the swf file. For my needs that was the most effecient.

Thanks so much for all the help. Your answers were straight forward and very detailed.

Upvotes: 0

Branden Hall
Branden Hall

Reputation: 4468

So this is a case of CS4 being too smart for it's own good. Since you aren't ever explicitly referring to the classes in question in a way the compiler can determine at compile-time it doesn't include those classes in your SWF - hence the error.

In Flex you can get around this with a command line argument, but that's not the case with CS4. Instead you have the explicitly reference the classes you want to use at least once in a manner that the compiler can understand. For example, I made a simple SWC that contained a symbol with it's class set to "TestCircle". In the code that utilized the SWC I had the following code and it worked just fine:

import flash.utils.getDefinitionByName;

TestCircle;

var test:MovieClip =  new (getDefinitionByName("TestCircle") as Class)();

addChild(test);

If you felt like getting fancy you could write a little script that would crack open a SWC (it's just a zip file), read the XML manifest that's inside and create a special "includer" class automatically. But as they say - I'll leave that as an exercise for the reader! ;-)

Hope that helps!

Upvotes: 4

Branden Hall
Branden Hall

Reputation: 4468

Rather than using a SWF, it's better to use a SWC. To create a SWC just go into the publish settings of Flash Pro and go to the Flash tab and select "Export SWC". The SWC contains both the SWF and a special manifest with all of the information Flash needs to know about the classes in the SWF.

Then, to use the SWC in another project you go to the AS3 settings (also in the Flash tab of publish settings), head to the Library Path tab and add your SWC (both absolute and relative paths work fine).

Note that this will only work in CS4 or Flex (With Flex I'm fairly sure you can just drag the SWC into your project, but I'll check on that).

Now - if you wanted to load the SWF and use it's assets at runtime, that's a whole different ball of wax. For that to work properly you'll need to fiddle around with the Loader class and ApplicationDomain so that the classes get put into the proper domain. Here's some good info on the subject:

http://www.kirupa.com/forum/showpost.php?p=2123134&postcount=366

Upvotes: 4

Related Questions