The web site for Flash ActionScript 3.0 game developers

 
         
   

Class or Object By Name in AS3

Hi Gary, just wondering if I can convert string to class/object in AS3?
I know Java can convert string name to class or object...
regards
Ed

Hello Ed. Yes, you can do this. The secret is the getDefinitionByNamefunction. Here is an example:

var classRef:Class = getDefinitionByName("MyClassName") as Class;
var myObject:Object = new classRef();

So myObject can be a Sound, a MovieClip, whatever. It is amazing how little attention this function gets when it is absolutely necessary for programming anything that uses a large library of named objects like sounds, graphics, etc.



Copyright Gary Rosenzweig