try this:

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

You were having 2 problems.  First, let's say that you have 7 children and
remove 5.  By the end of the loop, the child with the highest index will
have an index of 1 (0 indexed), yet the loop will be trying to access the
child at 6.  Since the child indexes only go up to 1 this will throw a range
error.  If you go from the highest index down, you will always be inside the
legal range because you can't remove faster than you decrement the counter.

The second problem is this:  Let's say you had children A, B, C at indexes
0, 1, 2.  You remove child and index 0 and then increment your counter.
During the next loop the children will be B, C and the child at index 1 is
C.  You've missed checking B.  If you go from the highest index down, you
don't have this problem.

I don't think I explained this very well.  Every once in a while the verbal
part of my brain stops working well with code.  Hopefully it was good
enough.

Dan Freiman
nondocs <http://nondocs.blogspot.com>

On 5/25/07, tw1l1ghtsh0ck <[EMAIL PROTECTED]> wrote:

  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? Thanks flexcoders!

Reply via email to