鼠标点击as3代码无效后,从舞台上删除生命

时间:2015-05-18 09:51:19

标签: actionscript-3 flash

我在舞台上的容器上添加了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);

  }

1 个答案:

答案 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包含至少一个孩子,请删除该孩子。