Getting the class of a DisplayObject in AS3

Handy wee snippet:

Class(getDefinitionByName(getQualifiedClassName(myMC)))

(Remember to import flash.utils)

Then you can bang it out like-a-so:

var mc:MovieClip = new targetClass();

eg. this below, gets the class of an instance called ‘page0’ within contentMC, creates a new one and adds it to the stage:

var targetClass:Class = Class(getDefinitionByName(getQualifiedClassName(contentMC.page0))); 
var tempPage:DisplayObject = new targetClass();
addChild(tempPage);

And if you’re just after referencing a class in your library by a string or something for instance, you can use this:

 var ClassReference:Class = getDefinitionByName(gameData.level[game.level].background.backgroundLayer) as Class;
      var bg = new ClassReference(); 
Tech Reference:

Leave a Reply

Your email address will not be published. Required fields are marked *

Proudly powered by WordPress | Theme: Baskerville 2 by Anders Noren.

Up ↑