6 Replies Latest reply on Aug 20, 2007 12:23 PM by jfillman

    For Loop Confusion

    jfillman Level 1
      I'm trying to remove the children in a container above where numChildren > 98. To me, this seems correct, but it always leaves 1, and sometimes 2 children above the 98 threshold.

      var numChildren:Number = myCanvas.numChildren;
      if (numChildren > 98){
      for (var i:int=98;i < numChildren;i++){
      myCanvas.removeChildAt(i);
      numChildren = numChildren-1;

      }
      }
        • 1. Re: For Loop Confusion
          jfillman Level 1
          if (numChildren > 98){
          for (var i:int=98;i < numChildren;i){
          myCanvas.removeChildAt(i);
          numChildren = numChildren-1
          }
          }
          • 2. Re: For Loop Confusion
            ntsiii Level 3
            A general solution to this is to delete the items by looping in reverse. this way the indexes do not change during the loop.
            for (var i:int=numChildren-1;i == 98;i--){

            Tracy
            • 3. Re: For Loop Confusion
              jfillman Level 1
              Tracy,

              This ends up not removing any children. On a related, but seperate question, how would I remove event listeners for those children within this loop?

              Jason
              • 4. Re: For Loop Confusion
                flexPro Level 1
                Tracy made a small typo, but the idea is right. Simply replace the test i == 98 in the for loop with i > 98 (you want to loop for all but the first 98 children).

                Regarding removing event handlers, try the following:

                for(var i:int = numChildren - 1; i > 98; i--) {
                var child:UIComponent = UIComponent(getChildAt(i));
                child.removeEventListener("eventName", listener);
                removeChildAt(i); // or removeChild(child);
                }
                • 5. Re: For Loop Confusion
                  flexPro Level 1
                  Perhaps I should note that you should replace "eventName" and listener with the event name and listener function that you want to remove for the item.
                  • 6. Re: For Loop Confusion
                    jfillman Level 1
                    Hmmm, this still isn't quite right. I kept getting errors, so I seperated this out.

                    First, this:

                    for(var i:int = numChildren - 1; i > 98; i--) {

                    removes child 100 and greater, but leaves the 99th child.

                    Second, this:

                    var child:UIComponent = UIComponent(getChildAt(i));

                    gives this error: "RangeError: Error # 2006: The supplied index is out of bounds."

                    I assumed that if I fixed the first error, the second would be corrected, but when I put my logic from the second post (which does remove all children above 98, but I prefer doing this in the for syntax as you and Tracy have suggested), the second error still remains.