[AS3] Add Class to Stage using String

Was working on a project today and stucked and wondering how you can actually place a large number of custom classes in your Library and then call it out via a name that is formed out of a string?

Example: … “mcApple”+number+date

You can actually do it by using the method known as  “Class Reference”.

var yourString:String = “mcName”+”customName”+customData;
var className:Class = getDefinitionByName(yourString) as Class;
var mc:MovieClip = new className();

this.addChild(mc);

See? Doesn’t seem to be too hard and it’s really helpful especially when you’re still very much of a semi-developer/designer and need to access the library dynamically.

References: