开发者

Use a addEventListener dynamically?

开发者 https://www.devze.com 2022-12-16 06:58 出处:网络
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 soem

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?


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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消