Skip to main content
Participant
July 23, 2011
Answered

Multiple Instances for Button Actions...?

  • July 23, 2011
  • 1 reply
  • 1344 views

So I have the following Actions for my button:

b1.buttonMode = true;
b1.addEventListener(MouseEvent.ROLL_OVER,onButtonOver);
b1.addEventListener(MouseEvent.ROLL_OUT,onButtonOut);

b1 is my MC button Instance name. I want to add several other MC button names to this ActionScript (b2, b3, etc). How can I add the other MC button names without just copying and pasting the whole code with new instances?

Can't we do something like:

b1 = b2 = b3.addEventListener(MouseEvent.ROLL_OVER,onButtonOver);

Thanks

This topic has been closed for replies.
Correct answer Ned Murphy

No, you cannot assign listeners in that manner.  But you can use a loop and bracket notation to do it...

for(var i:uint=1; i<=numBtns; i++){

   this["b"+String(i)].buttonMode = true;
   this["b"+String(i)].addEventListener(MouseEvent.ROLL_OVER,onButtonOver);
   this["b"+String(i)].addEventListener(MouseEvent.ROLL_OUT,onButtonOut);

}

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
July 23, 2011

No, you cannot assign listeners in that manner.  But you can use a loop and bracket notation to do it...

for(var i:uint=1; i<=numBtns; i++){

   this["b"+String(i)].buttonMode = true;
   this["b"+String(i)].addEventListener(MouseEvent.ROLL_OVER,onButtonOver);
   this["b"+String(i)].addEventListener(MouseEvent.ROLL_OUT,onButtonOut);

}

Participant
July 23, 2011

Thanks for the reply. I am quite the noob with this stuff. Could you possibly edit your code to reflect the following button instance names? b1, b2, b3

Once I see how you are adding the instance names within your code I will be good to go.

Thanks so much

Ned Murphy
Legend
July 23, 2011

The code already does that... so you should be good to go.  The only thing you need to define is the value of numBtns that the loop uses... if you only have 3 then assign it a value of 3.

var numBtns:uint = 3;