如何从一个swf实例类?

问题描述:

我有一个在我所设置为类库对象的FLA文件(在CS3中,用鼠标右键单击库中选择属性项,确保它被设置为输出的动作,并有一个类名)

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)

在本练习中,我们调用类MyClass的

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

如果我发布了FLA到SWC和SWF:

If I publish that FLA to an SWC and SWF:

我可以加载SWC静态和实例化MyClass的简单地做:

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

VAR研究所:MyClass的=新MyClass的();

var inst:MyClass = new MyClasS();

现在,问题:我希望能够在运行时做到这一点通过使用Loader对象加载的SWF文件

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

我知道如何访问已发布之前,在FLA创建手工实例,但我希望能够做的,就是创建一个类MyClass的新的实例。

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".

我可以得到一个影片剪辑重presenting SWF文件,我可以将其添加到我的图像显示,但我似乎无法得到在其中所含的类。 (我希望这是有道理的)

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)

有关如何攻击这个任何建议将是多少AP preciated。

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

要完成基督教的回答:

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.