views:

633

answers:

2

I have an FLA file with objects in the library which I have set to be "classes" (In CS3, right click an item in the library select properties, make sure it's set to export for actionscript, and has a class name)

For this exercise, let's call the class "MyClass"

If I publish that FLA to an SWC and SWF:

I can load the SWC statically, and instantiate "MyClass" by simply doing:

var inst:MyClass = new MyClasS();

Now, the problem: I'd like to be able to do this at runtime by loading the SWF file using a loader object.

I understand how to access instances which have been created by hand in the FLA before publishing, but what I want to be able to do, is create new instances of the class "MyClass".

I can get a "MovieClip" representing the swf file, I can add it to my displaylist, but I can't seem to get at the classes contained therein. (I hope this makes sense)

Any suggestions for how to attack this would be much appreciated.

+2  A: 

Have a look here; you should be able to extract a class reference by using Loader.contentLoaderInfo.applicationDomain.getDefinition("MyClass").

Christian Nunciato
+3  A: 

To complete Christian's answer:

var cls : Class = loader.contentLoaderInfo.applicationDomain.getDefintiion("ClassName");

var instance : Object = new cls();

Additionally, it's worth noting that you won't get strong typing (ie. it must be declared as Object) unless the class implements interface which is also defined in your main application. You will then be able to declare the instance variable as the interface and have compile-time access to it's members.

Richard Szalay