动态目标发起AS3 [英] Dynamic Object Initiation As3
问题描述
我注意到闪光旧版本,你可以创建一个动态类的实例。我创建一个游戏,将有可显示在舞台上,但可以很不时许多不同的类别。我怎么会去动态调用它们。例如:
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 ??
推荐答案
我认为有2种方式,你可以做到这一点:
I think there are 2 ways you can do that:
1.使用<一个href="http://help.adobe.com/en_US/AS3LCR/Flash_10.0/flash/system/ApplicationDomain.html#getDefinition%28%29"相对=nofollow>的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,使用<一个href="http://help.adobe.com/en_US/AS3LCR/Flash_10.0/flash/utils/package.html#getDefinitionByName%28%29"相对=nofollow> 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!