如何从 swf 实例化类? [英] How can I instantiate class from a swf?

查看:44
本文介绍了如何从 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 action-script, 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 inst:MyClass = new MyClasS();

现在,问题是:我希望能够在运行时通过使用加载器对象加载 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".

我可以获得一个代表 swf 文件的MovieClip",我可以将它添加到我的显示列表中,但我似乎无法获得其中包含的类.(我希望这是有道理的)

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.

格式代码

推荐答案

完成 Christian 的回答:

To complete Christian's answer:

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

var instance : Object = new cls();

此外,值得注意的是,除非类实现了也在主应用程序中定义的接口,否则您不会获得强类型(即它必须声明为 Object).然后,您就可以将实例变量声明为接口,并在编译时访问它的成员.

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.

这篇关于如何从 swf 实例化类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆