How can I instantiate a class from swf?

I have a FLA file with objects in the library that I set as “classes” (in CS3, right-click an item in the library, select properties, make sure it is set to export for actionscript and has a class name)

Call MyClass for this exercise.

If I publish FLA in SWC and SWF:

I can load SWC statically and create an instance of "MyClass" by simply doing:

var inst: Myclass = new MyClasS ();

Now the problem is: I would like to be able to do this at runtime by loading the SWF file using the loader object.

I understand how to access instances that were manually created in FLA before publishing, but what I want to do is create new instances of the "MyClass" class.

I can get a “MovieClip” representing the swf file, I can add it to my display list, but it looks like I can't get into the classes contained in it. (Hope this makes sense)

Any suggestions on how to attack this would be greatly appreciated.

+3
source share
2 answers

To complete the Christian answer:

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

var instance : Object = new cls();

In addition, it is worth noting that you will not get strong typing (that is, it must be declared as an object) if the class does not implement an interface that is also defined in your main application. Then you can declare the instance variable as an interface and access the compilation members.

+3
source

; , Loader.contentLoaderInfo.applicationDomain.getDefinition( "MyClass" ).

+2

All Articles