开发者

Dynamically Instancing Objects ActionScript 3.0

开发者 https://www.devze.com 2023-01-18 16:38 出处:网络
I have a variable named \"type\".And I want to instance an object with the name of the value of type.Here is an example:

I have a variable named "type". And I want to instance an object with the name of the value of type. Here is an example:

var myObjectName = "ball";
var object = new ball(); //Except I want to u开发者_JS百科se the value of myObjectName.

I believe this used to be easy with AS2 when using _global, but I'm not sure how to do it in AS3?

Any help?


First get the class object with flash.utils.getDefinitionByName(), then instantiate that object:

var myClass:Class = getDefinitionByName(myObjectName) as Class;
var object:Object = new myClass();

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/package.html#getDefinitionByName()

0

精彩评论

暂无评论...
验证码 换一张
取 消