8 Replies Latest reply on Nov 24, 2006 6:03 PM by kglad

    Listener help

    jgn2006 Level 1
      I ahve several button objects on stage and I am looking for a way to have them return their instance names or other properties without having to write a code for each. for example, for the button gaur_btn, I have

      gaur_btn.onPress = function ()
      {
      trace(gaur_btn._x);
      };

      which return the x position of the button. How do I make it generic so that I do not ahve to write this for each button? Otherwise I will have tor epeat this 100 times for the 100 buttons I have which just doesnt look right. Help appreciated.
        • 1. Re: Listener help
          Level 7
          One way would be to drop a generic piece of code on each instance like this:

          on(press) { _root.sendback(this._name, this._x); }

          Then in the actionscript frame on the root (in this example)

          function sendback(theName, theX) {
          trace (theName + " " + theX);
          //now you can refer to the instance name in a variable using brackets
          trace (_root[theName]._y);
          }

          Timm


          "jgn2006" <webforumsuser@macromedia.com> wrote in message
          news:ek2ag5$ig7$1@forums.macromedia.com...
          >I ahve several button objects on stage and I am looking for a way to have
          >them
          > return their instance names or other properties without having to write a
          > code
          > for each. for example, for the button gaur_btn, I have
          >
          > gaur_btn.onPress = function ()
          > {
          > trace(gaur_btn._x);
          > };
          >
          > which return the x position of the button. How do I make it generic so
          > that I
          > do not ahve to write this for each button? Otherwise I will have tor
          > epeat
          > this 100 times for the 100 buttons I have which just doesnt look right.
          > Help
          > appreciated.
          >


          • 2. Re: Listener help
            kglad Adobe Community Professional & MVP
            if you have your mouse handlers attached to your buttons, you could write a Button.prototype.onPress function that would work for all your buttons.

            however, because you can only have, at most, one onPress() and one on(press) associated with each button/movieclip you can't use the prototype method if many of your buttons have the code you showed.

            you could write a custom class that has an onPress function that extends the button.onPress method, but then all your buttons will need to be from that class. that may not be so onerous if all your buttons are instances of a handful of library buttons.
            • 3. Re: Listener help
              kglad Adobe Community Professional & MVP
              actually, you can do this and preserve your previous onPress handlers:

              • 4. Re: Listener help
                jgn2006 Level 1
                thanks kglad for your responses. I will try that.
                • 5. Re: Listener help
                  kglad Adobe Community Professional & MVP
                  you're welcome.

                  this works for movieclips too. but it adds an onPress handler for all movieclips.

                  i'm still thinking about code for extending the onPress for ONLY movieclips that already have an onPress handler.
                  • 6. Re: Listener help
                    jgn2006 Level 1
                    kglad, this seems to work and a pretty good way of adding that to every button. However I will need to create another class for a few graphics that I would like to have an alternate onPress event as it seems this can have no exception. Would a similar thing work for onRollover?
                    • 7. Re: Listener help
                      jgn2006 Level 1
                      Yes that worked for onRollOver too. Any way of making an exception?
                      • 8. Re: Listener help
                        kglad Adobe Community Professional & MVP
                        you can have an exception using instance names. if you want to have an exception for a subclass of buttons, you would be better off to scrap the above and create your own handlers.

                        here's code allowing an exception for a button with instance name "btn", regardless of timeline: