views:

26

answers:

1

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 use 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?

+3  A: 

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()

Claus Wahlers