Reputation: 30158
I'm trying to remove children from a movie clip upon user interaction but it's saying "the supplied index is out of bounds" - however, I must be missing something because it doesn't seem like anything is out of bounds. This is my code:
Circle.as:
for (var i=0; i<3;i++){ //this number should be based on the number of children found in the XML
var wedge:Wedge = new Wedge(wedgeHolderRef, i, cr,cScale);
}
Wedge.as:
public function Wedge(wedgeHolderRef, wedgeNum:int, cr:int = 3, cScale:int = 5)
{
var wedge:Sprite = new Sprite();
var wedgeClip:MovieClip = new MovieClip();
wedgeClip.addChild(wedge);
wedgeHolderRef.addChild(wedgeClip);
}
private function wedgeClickHandler(wedgeRef):void
{
var wedgeChildren = wedgeHolderRef.numChildren;
for (var i=0; i<wedgeChildren; i++)
{
var wedgeClip = wedgeHolderRef.getChildAt(i);
if (i != wedgeChildren-1){
} else {
deactivateCircle();
}
}
}
private function deactivateCircle()
{
var wedgeChildren = wedgeHolderRef.numChildren;
for (var i=0; i<wedgeChildren; i++){
//trace (i + "|" + wedgeHolderRef.getChildAt(i).wedgeNum);
wedgeHolderRef.removeChildAt(i); //Here is where I get the error
}
}
Upvotes: 0
Views: 1827
Reputation: 15955
While you are removing children, your index does not account for the number of children decreasing.
After one iteration, numChildren drops by 1 but your index remains based upon the original child count.
while (wedgeHolderRef.numChildren > 0)
wedgeHolderRef.removeChildAt(0);
Upvotes: 1
Reputation: 4870
You should not iterate from 0 to the number of children you stored there because i
will go higher than the actual number of children at some point. The number of children decreases every time you're removing one.
Instead do this:
while(wedgeHolderRef.numChildren)
{
wedgeHolderRef.removeChildAt(0);
}
Upvotes: 2