我在舞台上的容器上添加了5个生活图标..现在我想通过鼠标点击逐个删除它们。它正在第一次点击工作(一个孩子正在从舞台上移除)。但是它不能用于那我该怎么办。代码中没有错误但没有工作。代码是
var Lives:Number = 5;
var Spacing:Number = 5;
var nextX:Number = 0;
for(var i:int = 0; i < Lives; i++ )
{
var mc:MovieClip = new mcPlayerLives();
mc.x = nextX;
lives_container.addChild(mc );
nextX += mc.width + Spacing;
}
attackButton.addEventListener(MouseEvent.CLICK, removeLife);
function removeLife(event:MouseEvent):void
{
// Lives= Lives - 1;
if (lives_container.contains(mc))
lives_container.removeChild(mc);
}
答案 0 :(得分:0)
您已在循环中定义变量mc
。当循环结束时,mc
指向mcPlayerLives
类的最后一个实例。在removeLife
函数中,您只删除mcPlayerLives
类的最后一个实例。
而不是
if (lives_container.contains(mc))
lives_container.removeChild(mc);
使用
if (lives_container.numChildren)
lives_container.removeChildAt(0);
这意味着:如果lives_container
包含至少一个孩子,请删除该孩子。