i have added 5 life icons on a container in stage..now i want to remove them one by one by mouse click.it is working for first click(one child is removing from stage).but it is not working after that.what should i do.there is no error in code but not working.here is the code
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);
}
You've defined the variable mc
in the loop. When loop end, mc
point to the last instance of the mcPlayerLives
class. In the removeLife
function you remove only last instance of the mcPlayerLives
class.
Instead of
if (lives_container.contains(mc))
lives_container.removeChild(mc);
use
if (lives_container.numChildren)
lives_container.removeChildAt(0);
It means: if lives_container
contains at least one child, remove the child.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.