2 Replies Latest reply on Jan 17, 2008 8:51 AM by brian914

    The # of dynamically created button

    brian914 Level 1
      I have just lately started to assign my button actions dynamically. I have doing something like this:

      for(var i:Number = 0; i < thumbs; i++){
      thumb_scroller.all_thumbs["thumb" + i].bdr._alpha = 0;
      var ref:MovieClip = thumb_scroller.all_thumbs["thumb" + i];
      ref.onRollOver = function():Void {
      btnRollOver(this, btnFadeTime1);
      }
      ref.onRollOut = function():Void {
      btnRollOff(this, btnFadeTime1);
      }
      ref.onRelease = function():Void {
      clickAction();
      resetThumbBtns(this);
      }
      };

      I now have an array populated from an xml document. Each button when clicked should display its corresponding string from that array. For example, when I click on button 5 I want to show string five from myArray. This will go into the clickAction function. What I don't know how to do, is give each button that number just by itself so I can call that from the array. I tried clickAction(i); but obviously i gets updated and then is one number and not a different one for each button.

      How can I do this? I know I could do it with an ifStatement, but it would create tons of code, as there are 50 buttons.

      Thank you so much for any help with this.
        • 1. Re: The # of dynamically created button
          dr_ross Level 1
          you could do a it in a could of ways, either by using the name of the button as the id , i.e. if all the buttons are called like "btn1", "btn2".. etc then just use substr on the name property to strip of the "btn" and parse the characters left to numbers.

          Or the quickest way would be to just embed a variable in each button in the for loop storing the value of i

          i.e.

          for(var i:Number = 0; i < thumbs; i++){
          thumb_scroller.all_thumbs["thumb" + i].bdr._alpha = 0;
          var ref:MovieClip = thumb_scroller.all_thumbs["thumb" + i];

          //store variable inside button
          ref.iNum = i

          ref.onRollOver = function():Void {
          btnRollOver(this, btnFadeTime1);
          }
          ref.onRollOut = function():Void {
          btnRollOff(this, btnFadeTime1);
          }
          ref.onRelease = function():Void {
          //use this.iNum to refer to the variable stored inside this/ref
          trace(this.iNum);
          //then pass this.iNum where ever you like
          clickAction(this.iNum);
          resetThumbBtns(this);
          }
          };
          • 2. Re: The # of dynamically created button
            brian914 Level 1
            Thank you very much. I added the variable, which is what I was looking to do, but just did not know how to.