views:

61

answers:

1

I have created a navbar in flash with 5 different movieclips I use as buttons. Each movieclip(button) has a different instance name. Is there a way to use addeventlistener so that I dont have to do soemthing like this:

//for button1
 button1.buttonMode = true;// Show the hand cursor
button1.addEventListener(MouseEvent.ROLL_OVER, button1_over);
button1.addEventListener(MouseEvent.ROLL_OUT, button1_out);
button1.addEventListener(MouseEvent.CLICK, button1_click);

function button1_over(e:MouseEvent):void
{
    e.currentTarget.gotoAndPlay("over");
}

function button1_out(e:MouseEvent):void
{
    e.currentTarget.gotoAndPlay("out");
}

function button1_click(e:MouseEvent):void
{
    var request:URLRequest = new URLRequest("http://website.com");
    navigateToURL(request);
}
//for button2
button2.buttonMode = true;// Show the hand cursor
    button2.addEventListener(MouseEvent.ROLL_OVER, button2_over);
    button2.addEventListener(MouseEvent.ROLL_OUT, button2_out);
    button2.addEventListener(MouseEvent.CLICK, button2_click);

    function button2_over(e:MouseEvent):void
    {
        e.currentTarget.gotoAndPlay("over");
    }

    function button2_out(e:MouseEvent):void
    {
        e.currentTarget.gotoAndPlay("out");
    }

    function button2_click(e:MouseEvent):void
    {
        var request:URLRequest = new URLRequest("http://website.com");
        navigateToURL(request);
    }

...and so on for all five buttons?

+5  A: 
function buttonOver( e:MouseEvent ):void {
  e.currentTarget.gotoAndPlay('over');
}
... etc

for each( var b:MovieClip in [button1,button2,button3,button4,button5] ) {
  b.addEventListener( MouseEvent.ROLL_OVER, buttonOver );
  b.addEventListener( MouseEvent.ROLL_OUT, buttonOut );
  b.addEventListener( MouseEvent.CLICK, buttonClick );
}

You could even further improve it by gleaning the type of event inside the function and just have the one:

function buttonHandler( e:MouseEvent ):void {
  // see the docs for MouseEvent and figure
  // out what string to pass to goToAndPlay
}
thenduks
button = movieclip
Jack Null
Yea sorry it wasn't obvious what the class of the object you were trying to add listeners to was. But yes, whatever type `button1` through `button5` are goes in the `for each`. Cheers!
thenduks