3 Replies Latest reply on Jan 8, 2011 10:18 PM by kglad

    Defining actions for buttons inside a for loop

    vb_k

      Hi, me again ^^
      The situation this turn is:

       

      I got a set of buttons and  a text  field where they load the content.
      I'm trying to not make one zillion functions and keep the code neat so i  guessed i would try set the buttons' actions within the loop too. I firstly had  some problems getting the i counter inside the onRelease function (actually I  don't think i ever got how to do it). But I tried this and worked:
      stop();
      var comprarXML = new XML();
      comprarXML.ignoreWhite = true;
      comprarXML.load('flash/comprar.xml');
      comprarXML.onLoad = checkLoading;
      function checkLoading(success) {
      rootNode = comprarXML.firstChild;
      ButtonReleaseAndContent ();
      }
      function ButtonReleaseAndContent (){
      for (i=0;i<12;i++){   // my buttons are not generated dynamicly, that  why the count.
      btTemp = eval ('content.test.bt'+i);
      btTemp.txt.htmlText =  rootNode.childNodes[i+1].firstChild.firstChild.nodeValue;
      btTemp.temp =  rootNode.childNodes[i+1].childNodes[1].firstChild.nodeValue;
      btTemp.onRelease = function (){
      content.test.test.htmlText = this.temp;  //// the problematic line
      }
      }
      }
      At the line where I set the text, I had a though "uh so, I may have to  refer from within the button mc, since the generated code goes inside it!". So i  tried the this.temp , guessing that this would be equal to the button level. A  few seconds later i noticed my referencing for the text field wasn't coming from  within the button, but from the root. So I have no clue why that works. Anyone  knows what have I done?

        • 1. Re: Defining actions for buttons inside a for loop
          kglad Adobe Community Professional & MVP

          if btTemp is a movieclip you can assign dynamic properties (like temp).  you can then retrieve that property when your button is released.

          • 2. Re: Defining actions for buttons inside a for loop
            vb_k Level 1

            Thanks for the reply!

             

            I think I've failed to explain what I tried to ask.

             

            I understand what you have said , but what I don't understand is, on that line:

             

            content.test.test.htmlText = this.temp;  //// the problematic line

            If i swap the this.temp for  btTemp.temp (wich should reffer to each button's temp var... it's a set like bt0/bt1....) the text set is always from the last button.

            I know the i for the for loop dies as soon as it finishes, as it's just a control var for the loop, but what i really get is, saying that i have 5 nodes with the content, for all buttons is:

             

            content.test.test.htmlText = bt4.temp;

             

            Repeating that for each button.

             

            If i swap btTemp.temp for this.temp , it works perfectly. I'm not sure what is 'this' in that situation, and why btTemp inside the onRelease, wich in it's turn is inside the loop, would set the property to the last value from the loop.

             

            -.-

            • 3. Re: Defining actions for buttons inside a for loop
              kglad Adobe Community Professional & MVP
              function ButtonReleaseAndContent (){
              for (i=0;i<12;i++){   // my buttons are not generated dynamicly, that  why the count.
              btTemp = eval ('content.test.bt'+i);
              btTemp.txt.htmlText =  rootNode.childNodes[i+1].firstChild.firstChild.nodeValue;
              btTemp.temp =  rootNode.childNodes[i+1].childNodes[1].firstChild.nodeValue;
              btTemp.onRelease = function (){
              // if btTemp is a movieclip, "this" refers to the btTemp being released
              content.test.test.htmlText = this.temp; 
              }
              }
              }
              1 person found this helpful