views:

5140

answers:

5

**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);

+1  A: 

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

Branden Hall
A: 

Wow, that's a heck of a lot easier than all those embed commands. Too bad I can't seem to use classes imported this way and dynamically instantiate them.

This works after linking the swc file as a component instead of using my old embed commands.

var Body:MovieClip = new MovieClip1();

But I cannot build the class name dynamically.

var Definition:Class = getDefinitionByName("MovieClip1") as Class; //undefined var Body:MovieClip = new Definition();

However, If the movieclip and it's class definition is in the library of my current fla instead of part of the swc, this does work.

var Definition:Class = getDefinitionByName("objectOnStageAndExported") as Class; var Body:MovieClip = new Definition();

I even tried this code which was working before. It looks up the class name in a two dimensional array of strings, then uses that to instantiate. I swear this was working even without the getDefinitionByName() utility. I don't supose I could replace the "this" with some other identifier which points to the swc?

Definition:Class = this[CreatureParam[Type][Mark2_cb + 2]] as Class; var Mark:MovieClip = new Definition();

I even tried messing around with getFullyQualifiedName() but had no luck with this either. Tracing the results of this just says [string] which is annoying cause I want the contents of the string.

+2  A: 

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!

Branden Hall
A: 

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.

+1  A: 

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.

Cameron Yule