8 Replies Latest reply on Aug 11, 2009 11:35 AM by thepixelsmith

    Multiple buttons using one event listener?

    Pantone7459 Level 1

      I'm trying to figure out if it's possible to run multiple buttons using a single addEventListener so that I don't have to create extra code. For example, in the code below I have my instance name "btnCA" which is on my movie clip. It works great and there are no issues with this code, but I need to add more buttons. Does that mean I have to copy and paste this code for each button or is there a way to add multiple instances to an event listener?

       

      Sample code:

       

      buttonMode = true;
      useHandCursor = true;

       

      var rewind:Boolean = false;
      btnCA.addEventListener(MouseEvent.ROLL_OVER,fadingIn);
      btnCA.addEventListener(MouseEvent.ROLL_OUT,fadingOut);
      btnCA.addEventListener(MouseEvent.CLICK,gotoSite);

       


      function fadingIn(e:MouseEvent):void{
          btnCA.play();
          rewind = false;
      }

       

      function fadingOut(e:MouseEvent):void{
          rewind = true;
      }
      function gotoSite(e:MouseEvent):void{
          navigateToURL(new URLRequest("http://www.google.com"));
      }

       

      this.addEventListener(Event.ENTER_FRAME,revFrame);
      function revFrame(e:Event):void{
          if(rewind == true){
              btnCA.prevFrame();
          }
      }

        • 1. Re: Multiple buttons using one event listener?
          JoshSquires Level 1

          Yes, I do it all the time because as you said why have extra code that does essentially the same thing. Just add the following code to the top of your function

           

          var sender:senderClass = evt.target as senderClass;

          (where senderClass is the actual Class of the object)

          then you can reference the sender in such as way

           

                      switch(sender.name)
                      {

                          case "Button1":

                              do stuff

                          break;

                         case "Button2":

                              do stuff

                         break;

                    }

           

          -Josh

          • 2. Re: Multiple buttons using one event listener?
            polatkanfatih Level 1

            you should add listeners for other buttons by referencing the same functions. you dont need to rewrite the functions again

             

            btnCA1.addEventListener(MouseEvent.ROLL_OVER,fadingIn);
            btnCA1.addEventListener(MouseEvent.ROLL_OUT,fadingOut);
            btnCA1.addEventListener(MouseEvent.CLICK,gotoSite);


            btnCA2.addEventListener(MouseEvent.ROLL_OVER,fadingIn);
            btnCA2.addEventListener(MouseEvent.ROLL_OUT,fadingOut);
            btnCA2.addEventListener(MouseEvent.CLICK,gotoSite);

            • 3. Re: Multiple buttons using one event listener?
              Pantone7459 Level 1

              Hey Josh,

               

              Thanks for the reply. I'm still a little confused just because I've never used this switch method before. Would you mind showing an example using the code I supplied?

               

              Thanks

              • 4. Re: Multiple buttons using one event listener?
                Pantone7459 Level 1

                Hi polatkanfaith,

                 

                Thanks for the reply. I'm actually trying to avoid the multiple listeners if I can. Any other ideas?

                 

                Thanks

                • 5. Re: Multiple buttons using one event listener?
                  thepixelsmith Level 2

                  I don't know if this is easier or harder then using the switch() statement. Assuming you wanted the buttons to run the same function, couldn't you just store your button/MC names in an Array and then use a loop to assign the same event listener to them all. Something like this:

                   

                  var buttonArray:Array = new Array(button01_mc, button02_mc, button03_mc, button04_mc)

                   

                  this.loaderInfo.addEventListener(Event.COMPLETE, init);

                   

                  function init(myEvent:Event):void {

                      for(var i:Number = 0; i < buttonArray.length; i++){

                          buttonArray[i].buttonMode = true;

                          buttonArray[i].addEventListener(MouseEvent.MOUSE_DOWN, playMouseDown);
                      }
                  }

                   

                  function playMouseDown(myEvent:MouseEvent):void {

                       //statements

                  }

                  • 6. Re: Multiple buttons using one event listener?
                    JoshSquires Level 1

                    buttonMode = true;
                    useHandCursor = true;

                     

                    var rewind:Boolean = false;
                    btnCA.addEventListener(MouseEvent.ROLL_OVER,fadingIn);
                    btnCA.addEventListener(MouseEvent.ROLL_OUT,fadingOut);
                    btnCA.addEventListener(MouseEvent.CLICK,gotoSite);

                     

                    btnFL.addEventListener(MouseEvent.ROLL_OVER,fadingIn);
                    btnFL.addEventListener(MouseEvent.ROLL_OUT,fadingOut);
                    btnFL.addEventListener(MouseEvent.CLICK,gotoSite);


                    function fadingIn(e:MouseEvent):void{

                        var sender:Button = evt.target as Button;
                        sender.play();
                        rewind = false;
                    }

                     

                    function fadingOut(e:MouseEvent):void{
                        rewind = true;
                    }
                    function gotoSite(e:MouseEvent):void{

                          var sender:Button = evt.target as Button;

                                switch(sender.name)
                                {

                                    case "btnCA":

                                        navigateToURL(new URLRequest("http://www.google.com"));

                                    break;

                                   case "btnFL":

                                       navigateToURL(new URLRequest("http://www.structured-systems.net"));

                                   break;

                              }
                    }

                     

                    this.addEventListener(Event.ENTER_FRAME,revFrame);
                    function revFrame(e:Event):void{

                       var sender:Button = evt.target as Button;
                        if(rewind == true){
                            sender.prevFrame();
                        }
                    }

                    • 7. Re: Multiple buttons using one event listener?
                      polatkanfatih Level 1

                      using array is more logical, especially if you have many controller. however, you have to add listener for every button, otherwise, they wont do anything when you click...

                      • 8. Re: Multiple buttons using one event listener?
                        thepixelsmith Level 2

                        Basically you have to add an eventListener to every button regardless of which method you use. Switch allow you to repurpose the same function, while the loop doesn't make you write the eventListenter multiple times.