When writing a game or another such type of program which requires parsing save data to workable data, you often have to write parsers. More often than not you have to write a switch statement to parse, say, a piece of xml representing that object. This process can be made easier if you can have a node in your xml that contains the name of the Class you want to instantiate. And heres how to do that :
var myClassString:String = "MyClass"; var classReference:Class = getDefinitionByName(myClassString) as MyClass; var myNewInstance:MyClass = new classReference();
I’m using this method in a game engine i’m writing. I use it as part of my level parser as i have a large amount of library graphics in CS5 which all sub-class my LevelObject Class. This provides me an easy way to iterate through a list of objects in XML and instantiate them without having to write a ridiculous switch.
Peace
Bill




