How can I instantiate class from a swf?

六眼飞鱼酱① 提交于 2019-12-10 21:54:56

问题


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 action-script, 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.

Edit : Format code


回答1:


To complete Christian's answer:

var cls : Class = loader.contentLoaderInfo.applicationDomain.getDefinition("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.




回答2:


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



来源:https://stackoverflow.com/questions/467622/how-can-i-instantiate-class-from-a-swf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!