1 Reply Latest reply on May 26, 2007 12:01 AM by chris.huston.t10

    RangeError: Error #2006: The supplied index is out of bounds.

    yu.wei.qian.07
      I.
      var numChildren:Number = canvas.numChildren - 1;
      for (var i:int = 0; i < numChildren; i++) {
      if (canvas.getChildAt(i).visible == true) {
      canvas.getChildAt(i).visible = false;
      }
      }


      II.
      var numChildren:Number = canvas.numChildren - 1;
      for (var i:int = 0; i < numChildren; i++) {
      if (canvas.getChildAt(i).visible == true) {
      canvas.removeChildAt(i);
      }
      }

      The first block of codes works fine... However, with the second block it returns this error: 'RangeError: Error #2006: The supplied index is out of bounds.'

      Can somebody tell me what's wrong?
        • 1. Re: RangeError: Error #2006: The supplied index is out of bounds.
          chris.huston.t10 Level 3
          Because you are removing a child through each iteration of your for-loop, you need to adjust your index to account for the change. You could set your for-loop to remove children from the last child down to the first child like this:

          var numChildren:Number = canvas.numChildren - 1;
          for (var i:int = numChildren; i <=0 ; i--) {
          if (canvas.getChildAt(i).visible == true) {
          canvas.removeChildAt(i);
          }
          }

          Or you could remove the first child each time you go through the iteration like this:

          var numChildren:Number = canvas.numChildren - 1;
          for (var i:int = 0; i < numChildren; i++) {
          if (canvas.getChildAt(i).visible == true) {
          canvas.removeChildAt(0);
          }
          }

          Both of these approaches have the same result; removal of all the current children of the given canvas.

          Vygo