Dynamically create variable type using string value
Hi. In my project I created several classes named Square1, Square2, Circle 1, Circle2, Triangle1, Triangle2, etc. They all extend MovieClip and the create different shapes and have slightly different properties that set them appart.
There comes a point when I need to instance them and add the instances to the stage. I do this through buttons that have a sType:String property, that corresponds to the name of the class they should instantiate. So button S1 has sType = "Square1", and so on.
Now, what I would like to do is, instead of having a ton of handler functions for all the buttons, have only a single function that handles all the buttons and decides which class to instantiate. It would be something like this:
function buttonClicked ($event:MouseEvent):void {
var instanceClass:String = $event.target.sType; //let's say it's "Square1"
var newShape:MovieClip = new [instanceClass]();
}
Is this possible?
Thank you.