Search code examples
actionscript-3flashmovieclip

How can I target dynamic name of movieclip which is a child of another one


I'm trying to control a movieclip which is located in another movieclip, the problem is that the child movieclip has a dynamic instance name, please take a look for this example:

var myvar = "2";

mc_1.mc_2.y = 0; // that's ok

but if I try:

mc_1.this["mc_"+myvar"].y = 0;    

Syntax error: expecting identifier before this."`

I try:

this["mc_1.mc_"+myvar"].y = 0;

Error #1010: A term is undefined and has no properties.

and when I try that:

MovieClip("mc_1.mc_"+myvar").y = 0;     

Error #1034: Type Coercion failed: cannot convert "mc_1.mc_2" to flash.display.MovieClip.

one more try:

mc_1.MovieClip("mc_"+myvar").y = 0; 

Error #1006: MovieClip is not a function.


Solution

  • You can simply do :

    var myvar:String = '2';
    
    mc_1['mc_' + myvar].y = 0; 
    

    Hope that can help.