Skip to main content
Jhon Carlo
Inspiring
July 30, 2013
Question

Several buttons that do the same behavior

  • July 30, 2013
  • 2 replies
  • 586 views

Hello,

I need to have several buttons that do the same behavior.

For brevity, I do not want to attribute to them each time a different instance name.

Is there a way to perform many buttons the same function (eg trace ("foo");?

And:

1)  without converting all the buttons in a single symbol, this is because I often need to give different positions on the Stage;

2)  without to write a different script for each button?

Thank you.

John.

This topic has been closed for replies.

2 replies

Participant
August 20, 2013

You would want to use a for loop for your need and dynamically reference each button instance. In the below example, create 4 buttons on your stage at the root level and give them instance names of "btn1", "btn2", "btn3" & "btn4". Then insert the following on the root timeline.

stop();

for (var i:Number = 1; i <= 4; i++){

          _root["btn"+i].onRollOver = function(){

                    trace("foo"+this._name);

                    }

}

kglad
Community Expert
Community Expert
July 30, 2013

if you want to control each individually (eg, change their positions relative to each other), the easiest way is to assign different instance names for each.

they could all be children of the same parent movieclip and you could encode one handler for the parent so you don't have to encode more than one handler.