views:

36

answers:

2

How do I add a variable that varies with the dummy variable in a loop:

function resetAll(menuNum){
  trace(menuNum);
  for (i=0; i<=7; i++){
    if(menuNum != 1){
      menu_all_mc.this["btn_"+i].gotoAndStop("off");
    }
  }
}

this["btn_"+i] don`t work

I need pass the btn name like: btn_1 and next loop btn_2 ...

A: 

I'm strictly guessing ( I don't do Actionscript but JS ), but if your i variable stays the same then you'll need to use closures to capture and bind that i in place:

function resetAll(menuNum){
    trace(menuNum);
    for (i=0; i<=7; i++){
    (function(i){
        if(menuNum != 1){
            menu_all_mc.this["btn_"+i].gotoAndStop("off"); 
        }
    })(i);
    }
}

Let me know if that doesn't work. Also make sure the this keyword is referencing the correct execution context.

Edit: the this can't be referenced like that, are you sure it's not just menu_all_mc["btn_" +i] ? this isn't a property of an object unless you explicitly define it as such.

o = {};
trace( o.this==undefined )

would evaluate to true because it was never defined. this in a function scope will refer to the current execution context but do not prefix it with another object.

Can you clarify what object owns the .btn1, etc?

meder
Speculation in a language you don't know isn't extremely helpful.
Rex M
That was fast, thanks very much!
Mango
A: 
menu_all_mc.this["btn_"+i].gotoAndStop("off");

this cannot be a member of a class, I am guessing you want

menu_all_mc["btn_"+i].gotoAndStop("off");

Or even

menu_all_mc[this["btn_"+i]]gotoAndStop("off");
LiraNuna