Adding MovieClip's to the stage

This is what I am trying to do:

[AS]
var level = new “level_” + hero.lvl;
[/AS]

I remember similar methods of this worked for AS2 (when creating a class/instance), but with the whole new re-written language of AS3, I am not able to do so. I am trying to add a movie clip to the stage depending on the heros current level.

Can someone quickly please tell me of a workaround or solution to this. Thank you in advance (I pulled chunks of hairs out of my head already, haha)


import flash.utils.getDefinitionByName;

var classObj:Class= getDefinitionByName("level_" + hero.lvl);
var level:* = new classObj();

Thanks McGuffin!

It didn’t work the first time for me though… I looked at a sample script from the Actionscript 3 docs (I didn’t know of the getDefinitionClassByName() function just til you mentioned it) and tried this way instead:

[AS]
import flash.utils.getDefinitionByName;

var class_obj:Class = getDefinitionByName(“Level_” + hero.lvl) as Class;
var lvl:MovieClip = new class_obj();

[/AS]

Then it worked. I guess adding “as Class” made that big of a difference. Thanks again

Ah, forgot that, my apologies. Just so you know, you can also typecast like:


var class_obj:Class = Class(getDefinitionByName("Level_" + hero.lvl));

There are benefits to using this against “as”, and “as” over this method. Searching “typecasting actionscript 3” and you’ll turn up some good articles.