动态对象初始化 As3 [英] Dynamic Object Initiation As3

查看:30
本文介绍了动态对象初始化 As3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到在旧版本的 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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