mouseclick as3 代码不起作用后从舞台上删除生命
remove life from stage after mouseclick as3 code is not working
我已经在舞台上的容器上添加了 5 个生命图标..现在我想用鼠标一个一个地删除它们 click.it 正在为第一次点击工作(一个 child 正在从舞台上删除).但它在 that.what 之后不工作应该我 do.there 是代码中没有错误但不是 working.here 是代码
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);
}
您已经在循环中定义了变量 mc
。当循环结束时,mc
指向 mcPlayerLives
class 的最后一个实例。在 removeLife
函数中,您仅删除 mcPlayerLives
class.
的最后一个实例
而不是
if (lives_container.contains(mc))
lives_container.removeChild(mc);
使用
if (lives_container.numChildren)
lives_container.removeChildAt(0);
表示:如果lives_container
至少包含一个child,则去掉child。
我已经在舞台上的容器上添加了 5 个生命图标..现在我想用鼠标一个一个地删除它们 click.it 正在为第一次点击工作(一个 child 正在从舞台上删除).但它在 that.what 之后不工作应该我 do.there 是代码中没有错误但不是 working.here 是代码
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);
}
您已经在循环中定义了变量 mc
。当循环结束时,mc
指向 mcPlayerLives
class 的最后一个实例。在 removeLife
函数中,您仅删除 mcPlayerLives
class.
而不是
if (lives_container.contains(mc))
lives_container.removeChild(mc);
使用
if (lives_container.numChildren)
lives_container.removeChildAt(0);
表示:如果lives_container
至少包含一个child,则去掉child。