Actionscript 3 Class.forName
I'm sure this would be obvious for anyone that has been programming in Actionscript 3 for awhile, but coming in from the Java world I'm still trying to find the matching idioms. In particular I wanted a way to dynamically load a class based on its name. In Java this is the Class.forName() construct. Luckily AS3 also has a similar mechanism:
// return the class as a Class object var temp:Class = flash.utils.getDefinitionByName("com.example.Sample") as Class; // you can now call new it to create an instance along with passing constructor args var instance:Object = new temp(arg1, arg2, ...);
If when doing this you get an "Runtime Error 1065" see this post.
Note: Update Nov 6, 2007
It looks like flash.util.getClassByName() has gone away. The example above has been updated to use the new mechanism called flash.utils.getDefinitionByName().
Comments
Posted by: Sundev | October 24, 2007 3:56 PM
Posted by: Jackson | October 16, 2008 7:17 PM