我注意到在旧版本的 flash 中你可以创建一个动态类的实例.我正在创建一个游戏,该游戏将有许多不同的类,可以在舞台上展示,但不时可以.我将如何动态调用它们.例如
I notice in older version of flash you can create an instance of a dynamic class. I am creating a game that will have many different classes that can be displayed on the stage but can very from time to time. How would I go about calling them dynamically. for example
var newObject = new ["DynamicObject"]();??
这在 As3 中可行吗??
Is this possible in As3 ??
推荐答案我认为有两种方法可以做到:
I think there are 2 ways you can do that:
1.使用 ApplicationDomain.getDefinition('DynamicTemplate')
类似:
var DynamicClass:Class = this.loaderInfo.applicationDomain.getDefinition('DynamicTemplate') as Class; addChild(new DynamicClass);您需要在文件初始化后执行此操作.
You would need to do this when you file has INITialized.
2.使用 getDefinitionByName() :
var DynamicClass:Class = flash.utils.getDefinitionByName('DynamicTemplate') as Class; addChild(new DynamicClass);如果您需要一种获取类名称以创建新对象实例的方法,可以使用 describeType() 或通过实例的构造函数,但我认为无论如何你都会知道你的类.
If you need a way to get Class names to create new instances of objects, you could use describeType() or go through the instance's constructor, but I reckon you would know your classes anyway.
var TemplateObj:Class = flash.utils.getDefinitionByName(describeType(yourIntance).@name) as Class; var newObj = new TemplateObj(); var newObj2 = new yourIntance.constructor();希望对您有所帮助,乔治
Hope this help, George
更多推荐
动态对象初始化 As3
发布评论