4 Replies Latest reply on Oct 28, 2010 8:12 AM by Ned Murphy

    Dynamicaly create onRelease event

    SpiritOfTheWinds Level 1

      Hello,

       

      I got a problem with dynamicaly assigning the onRelease event on my buttons.

       

      I got about 45 buttons and I don't want to create 300 lines of code just to create each of the onRelease events on them so I thought about using a loop (for) to pass through them and assigning the onRelease event. This is what I got and thought would work:

       

      for (var s = 1; s <= _global.NB_SECTIONS; s++) // NB_SECTIONS = 42
      {
          this["btnSalle" + s].onRelease = function(){
              trace(s); // Returns 43
              _global.CURRENT_SECTION = s;
              _root.gotoAndStop("salle");
              [...]
          };
      }

       

      Unfortunately, it does go to "salle" (which is a frame), but the CURRENT_SECTION variable is always set to 43! I can't understand why it would give me 43 on any of the section buttons I click (which ranges from 1 to 42). Any idea how I could make this work? I don't like having too much codes in my Flash (I've been using a hell load of loops to make it cleaner).

       

      Thanks!

        • 1. Re: Dynamicaly create onRelease event
          Ned Murphy Adobe Community Professional & MVP

          The value of s is not assigned to the buttons, variable is coded into the functions and the function use the value of s at its current value when clicked, not when you assign it in the function.

           

          If those are button symbols, then you can use their _name(s) to determine the value you are looking for since the _name(s) carry the value you want...

           

          for (var s = 1; s <= _global.NB_SECTIONS; s++) // NB_SECTIONS = 42
          {
              this["btnSalle" + s].onRelease = function(){
                  trace(String(this._name).substr(8));

                  _global.CURRENT_SECTION =  Number(String(this._name).substr(8));
                  _root.gotoAndStop("salle");
                  [...]
              };
          }

           

          If they are movieclips used as buttons, then you could assign the s value to a variable ofeach movieclip and use that variable in the functions.

          • 2. Re: Dynamicaly create onRelease event
            srirama.83

            Name all the buttons like btn0,btn1.... so on.

             

            NumberOfBtns = 45;

             

            for (var i=0; i<NumberOfBtns; i++) {

                 this["btn"+i].onRelease = functionName;

            }

             

            function functionName() {

                 trace(this._name);//will trace(btn0,1... base on btn clicked

            }

             

             

            You can also use Delegate class

             

            Cheers

            Sriram

            • 3. Re: Dynamicaly create onRelease event
              SpiritOfTheWinds Level 1

              Oh indeed, it's a nice idea to use the button name. I should have thought about it.

               

              Also, you are right, these are movieclips used as buttons, but I'll stick to the previous way, works fine and is still the fast and simplier way.

               

              Thanks!