Skip to main content
Participant
November 20, 2009
Answered

same function for all mc buttons?

  • November 20, 2009
  • 1 reply
  • 897 views

I am putting together a simple flash anim – and pretty much have about 25 buttons that I need to fade in and out on rollover. I seem to remember there is a way to apply the same function to all buttons – as long as the over and out state labels within the mc are the same. Could someone let me know what i should look into in order to accomplish this? Thank you.

This topic has been closed for replies.
Correct answer

The loop just allows you to add your listeners to each button - btn1 - btn25. If you don't want to do it that way, then you can place all buttons into one container and just add listeners to the container... something like:

myButtonContainer.addEventListener(MouseEvent.MOUSE_OVER, showHighlight, false, 0, true);
myButtonContainer.addEventListener(MouseEvent.MOUSE_OUT, removeHighlight, false, 0, true);

function showHighlight(e:MouseEvent){
    e.target.gotoAndStop(2);
}
function removeHighlight(e:MouseEvent){
    e.target.gotoAndStop(1);
}

Note the use of target, instead of currentTarget in the listener functions. And you can use gotoAndPlay if you like... I just used gotoAndStop to illustrate.

1 reply

November 20, 2009

If you use a naming convention like btn1 - btn25 then you can use a for loop to add your listeners:

for(var i:int = 1; i < 26; i++){
    this["btn" + i].addEventListener(MouseEvent.MOUSE_OVER, showHighlight, false, 0, true);
    this["btn" + i].addEventListener(MouseEvent.MOUSE_OUT, removeHighlight, false, 0, true);
}
function showHighlight(e:MouseEvent){
    e.currentTarget.gotoAndStop(2);
}
function removeHighlight(e:MouseEvent){
    e.currentTarget.gotoAndStop(1);
}

Each button will then call showHighlight on mouse over, which will make that particular button goto and stop on frame 2 - the mouseOut will send it back to frame 1...

inventasAuthor
Participant
November 20, 2009

thank you for the help. Is there a more straight-forward way to do this? Sans the loop?

can i just use current target "gotoAndPlay" over / out?

Correct answer
November 20, 2009

The loop just allows you to add your listeners to each button - btn1 - btn25. If you don't want to do it that way, then you can place all buttons into one container and just add listeners to the container... something like:

myButtonContainer.addEventListener(MouseEvent.MOUSE_OVER, showHighlight, false, 0, true);
myButtonContainer.addEventListener(MouseEvent.MOUSE_OUT, removeHighlight, false, 0, true);

function showHighlight(e:MouseEvent){
    e.target.gotoAndStop(2);
}
function removeHighlight(e:MouseEvent){
    e.target.gotoAndStop(1);
}

Note the use of target, instead of currentTarget in the listener functions. And you can use gotoAndPlay if you like... I just used gotoAndStop to illustrate.