开发者

Dynamically Instancing Objects ActionScript 3.0

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜