1

I have 180 buttons in one Flash file and for all buttons I need a roll over and a click event, so the code I`m using is:

button1.onRollOver = function() { //on roll over change button color as white
  var color = new Color(button1); 
  color.setRGB("0xFFFFFF"); 
};
button1.onRollOut = function() { //on roll out reset button color to it`s default
  resetColorFunction(); 
};
button1.onPress = function() { //on click/press runs javascript function in page
  getURL("javascript:ButtonPress('button1');"); 
};

The problem is that I have 180 buttons, so I`m copying this code to each button, button1, button2, button3, button4 ...etc until button180.

Is there any way to loop through all buttons with one simple code, function.

Thank you

3 Answers 3

1

You can use a for-in loop in the stage or any container.

for (var item in this) {
    if (this[item] instanceof Button) { //-- Use the most relevent class for abstraction
        var btn = this[item];
        trace ("Button: " + btn._name + " btn._x:" + btn._x + " a: " + btn._alpha);

        //-- add Logic for event handlers.
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

FYI, @wzazza - ArrayCollection is part of the flex framework and hence AS3. (Could not comment on the other thread due to rep shortage).
0

You can use eval in Actionscript 2 to get all your buttons tagged with minimal effort.

buttonRollOver = function() { ... };
buttonRollOut = function() { ... };
buttonPressed = function() { ... };

for (var i=1; i<181; i++)
{
   eval("button"+i).onRollOver = buttonRollOver;
   eval("button"+i).onRollOut = buttonRollOut;
   eval("button"+i).onPress = buttonPressed;
}

And there you have it all nice and fast.

3 Comments

This looks promising :) The problem with this method is that the hover function affects only the last button (button180). No matter which button I hover the function affects last button not the button I hover. And I have created function inside for method like: eval("a"+i).onRollOver = function(){ var buttonColor = new Color(eval("a"+i)); buttonColor.setRGB("0xFFFFFF"); };. How to make it work for for each button separately?
The onRollOver function is being called with the target being your button itself. So if you want to modifiy the button you can just use this inside the function to affect it. For your example, you can just use eval("a"+i).onRollOver = function(){ var buttonColor = new Color(this); buttonColor.setRGB("0xFFFFFF"); };
this["button"+i].onRollOver = buttonRollOver; should also work directly so no need for eval
0

You can add all of these buttons to a collection of some sort, then loop through all of the elements and add the event handlers. Something like this:

var buttons:ArrayCollection = new ArrayCollection();
var button1:Button = new Button();
var button2:Button = new Button();
// some more buttons creation ...

and each time you create a button, you simply add it to the buttons collection.

buttons.addItem(button1);
buttons.addItem(button2);
// etc

Finaly you just loop through all the buttons and add the required event handlers.

for each (var button:Button in buttons)
{
    button.onRollOver = function() { // handle RollOver };
    button.onRollOut  = function() { // handle RollOut };
    button.onClick    = function() { // handle Click };
}

To use an array you can do the following:

var buttons:Array = [];
buttons.push(button1);
// add all other buttons..

for (var i:uint = 0; i < buttons.length; i++)
{
    var button:Button = Button(buttons[i]);
    button.onRollOver = function() { // handle RollOver };
    button.onRollOut  = function() { // handle RollOut };
    button.onClick    = function() { // handle Click };
}

9 Comments

well i guess this is the way to do it, but isn't there some easy way to loop automatically trough all buttons without creating ArrayCollection something like: for each (findAllButtonsInMovie) { //code for each button }
As far as I know, there's no such thing built in the language. Besides, the changes you have to make to your code seem like just adding 2 lines - one to declare the collection, and the other to add the buttons in it (assuming you're creating the buttons in a loop).
Im trying to use ArrayCollection as you suggested but I get this error: The class or interface 'mx.collections.ArrayCollection' could not be loaded.` what i`m missing here?
Do you have the proper import statement? You can use just an Array otherwise.
Can you post some simple code example by using Array? I just cant make it work. I`m using AdobeFlash CS5.5 with ActionsScript2.0
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.