Question : Using variables to create objects

I am presenting different characters with different moods.  For example, I would like to instantiate the movie clip, "mc_Jack_happy," whenever the mood = happy and the hero = Jack.  There are different movie clips for mc_Jack_sad and mc_Jane_happy, so it's not completely generic.

I'd like to use just these four lines to define my movie clip variable and place it.  While I've made good use of "this[mood + hero]", I would really like to replace "mc_Jack_happy" with the text in the comments.  Can this be done?  I've tried a variety of ways to use a variable in that place.  These variables are already declared, so that is not the issue.

Is it even possible for this to happen?
Code Snippet:
1:
2:
3:
4:
this[mood + hero] = new mc_Jack_happy();                    /*this["mc_" + hero + "_" + mood](); */
this[mood + hero].x = 820;
this[mood + hero].y = 24;
addChild(this[mood + hero]);
Open in New Window Select All

Answer : Using variables to create objects

Try the following:



-V
1:
2:
3:
4:
this[mood + hero] = new (getDefinitionByName("mc_" + hero + "_" + mood) as Class)();
this[mood + hero].x = 820;
this[mood + hero].y = 24;
addChild(this[mood + hero]);
Open in New Window Select All
Random Solutions  
 
programming4us programming4us