Removing children that are instances of a certain class

Hi. I have a class, called fCont (I know, I should use capital letters for class names… oh well), and I need to remove all instances of it from my MovieClip.

I’m making a Facebook browser for a uni assignment. When you go to the Friends page, it loads 15 friends, and that’s fine. The code below is called on the “Next Page” button press.


function nextFriendsPage(e:Event){
            var friendFace:Object = stage.getChildAt(0);
            var friendCount=friendFace.numChildren;
            trace ("numChildren: " + friendCount);
            var count:int = 0;
            while (count<=friendCount){
                var curObject:Object = friendFace.getChildAt(count);
                trace ("
count : " + count + "
curObject: " + curObject);
                if (curObject is fCont){
                    friendFace.removeChildAt(count);
                    //trace("curObject is fCont");
                }
                //trace ("count: " + count);
                count++;
            }
            friendsPageNum ++;
            drawFriends(friendsPageNum);
        }

Now for some reason, my var curObject:Object = friendFace.getChildAt(count); line returns an out of bounds exception once count hits 28, even though I’ve traced out numChildren and it’s 35. Now, I’m no maths guru, but I’m pretty sure 28 is less than 35 :h:

The exact error is:


RangeError: Error #2006: The supplied index is out of bounds.
    at flash.display::DisplayObjectContainer/getChildAt()
    at FriendFace/nextFriendsPage()

I don’t understand how getChildAt could be out of bounds inside a loop that only runs so long as count is less than the number of children… can anybody tell me why that’s happening?

Cheers guys.
Benjamin.