我试图在用户交互时从电影剪辑中删除子节点,但是它说“提供的索引超出范围” - 但是,我必须丢失一些东西,因为它似乎没有任何东西超出范围。这是我的代码:
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
}
}
答案 0 :(得分:2)
您不应该从0迭代到您存储在那里的孩子数,因为i
会比某个时候的实际孩子数高。每次移除儿童时,孩子的数量都会减少。
而是这样做:
while(wedgeHolderRef.numChildren)
{
wedgeHolderRef.removeChildAt(0);
}
答案 1 :(得分:1)
在删除子项时,您的索引不会考虑子项数量的减少。
一次迭代后,numChildren会减1,但您的索引仍然基于原始子计数。
while (wedgeHolderRef.numChildren > 0)
wedgeHolderRef.removeChildAt(0);