1 Reply Latest reply on Jan 4, 2007 1:50 PM by elementus

    Assigning actions to buttons added dynamically.

    elementus
      Hello,

      here is my problem, I am adding buttons to the stage run-time and for the life of my I can't get actions in them them to work. Now when i click the button nothing happens. Am I missing something??

        • 1. Re: Assigning actions to buttons added dynamically.
          elementus Level 1
          Well it seems i found the reason why. For starters I dropped the whole listener thing.

          What I was trying to accomplish was to select an image that was associated with the button number so say "image_1" has "button_1". So what i wanted was that when i clicked "button_1" it would send the number to the showImage() function.

          NOTE: I found out after a few hours that I could not just add the x variable to the function as in...

          this.adButton.onRelease = function()
          {
          showAdImage(x);
          }

          ... because it would recall the then current value of x which would end up being someNum. So FYI, calling events will call the then current values of all variables.

          SO MY SOLUTION...
          The way i got around it was by simply calling up the buttons name with "this" and turning it into a string variable. I then split it using split() and grabbed the number.

          Once that was done all worked fine.

          This is the final code.

          ...

          for (x=0; x < someNum; x++) {

          // name and create the button...
          btnName = "adButton_" + x;
          this.attachMovie("btn", btnName, this.getNextHighestDepth());

          // get button...
          adButton = eval(btnName);

          // position button...
          this.adButton._x = somePosX;
          this.adButton._y = somePosY;
          this.adButton._width = someWidth;

          // set and add actions for button...
          this.adButton.onRelease = function() {
          imThis = this + "";
          i = imThis.split("_level0.adButton_");
          showAdImage(i[1]);
          }
          }

          ...