1 Reply Latest reply on May 18, 2006 3:43 PM by blemmo

    Variable with nested function

    kanag
      I create a button in a loop, this means there is a button at each iteration. I pass variables to create it and then I add a function to respond when clicked.

      Here is what the code looks like:
      button = _root.attachMovie(...);
      button._x = xofs;
      button._y = 200;
      ...

      button.onRelease = function() {
      trace("button clicked, surl: "+surl);
      };

      The problem is that when I test the movie, they always give the same url no mather wich one I click. The variable surl is also changing at each iteration.

        • 1. Variable with nested function
          blemmo Level 1
          The line
          trace("button clicked, surl: "+surl);
          uses the value of 'surl' at the time it is executed, so it takes the last value assigned to 'surl'. You would have to store the proper value with the MC:

          button.link = surl;
          ...
          button.onRelease = function() {
          trace("button clicked, surl: "+this.link);
          };

          or pass the 'surl' variable by value, using a function:

          function defineRelease(mc, url){
          mc.onRelease = function(){
          trace("url = "+url);
          }
          }
          // in the loop:
          defineRelease(button,surl);

          hth,
          blemmo