2 Replies Latest reply on Sep 8, 2010 7:41 AM by ccay

    catch MouseEvent.CLICK current value in for-loop?

    ccay

      How do I catch the current value of a variable that is defined in a for-loop?

       

      I have a for-loop that loops through an array collection and for every element in it creates a LinkButton that has a click-event.

      However, the value I want to send with the event always ends up being the last one in the array collection and not the one that is currently defined when the eventListener is created... (am I making sense?)

       

      I am using the following code:

       

                     var ACobj:Object = parametersAC[i];

                     var paramNameLink:LinkButton = new LinkButton();


                     paramNameLink.addEventListener(MouseEvent.CLICK,
                          function():void {
                              launchPopUp(ACobj.name);
                          }
                     );

       

                     addChild(paramNameLink);

       

      It is the ACobj.name that always ends up being the same as the very last value of the .name attribute in the ACobj object.

       

      Is there any other way to do this?

        • 1. Re: catch MouseEvent.CLICK current value in for-loop?
          Gregory Lafrance Level 6

          Very strange. For anyone wanting to see a full working example:

           

          <?xml version="1.0"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
            creationComplete="init();">
            <mx:Script>
              <![CDATA[
                import mx.collections.ArrayCollection;
                import mx.controls.Alert;
                import mx.controls.LinkButton;
                
                private var parametersAC:ArrayCollection = 
                  new ArrayCollection([
                    {name: "one", label: "Label One"},
                    {name: "two", label: "Label Two"},
                    {name: "three", label: "Label Three"}
                  ]);
                private function init():void{
                  for(var i:int=0;i<parametersAC.length;i++){
                    var ACobj:Object = parametersAC.getItemAt(i);          
                    var paramNameLink:LinkButton = new LinkButton();
                    paramNameLink.label = ACobj.label;
                    paramNameLink.addEventListener(MouseEvent.CLICK,
                      function():void {
                        launchPopUp(ACobj.name);
                      }
                    );
                    addChild(paramNameLink);
                  }
                }
                
                private function launchPopUp(str:String):void{
                  mx.controls.Alert.show(str);        
                }
              ]]>
            </mx:Script>
          </mx:Application>
          
          • 2. Re: catch MouseEvent.CLICK current value in for-loop?
            ccay Level 1

            Thanks so much for such a thorough test of my code!

             

            That is almost exactly what my code looks like (although simplified)... But it is not called in the same way

            creationComplete="init();"

            but from the parent container as compList.displayList(parametersAC).

             

            Maybe the problem is that it has not been initialized properly first?