我正在尝试从舞台中删除数组的动画片段。每次从方形数组中的项目中单击时,我想删除圆形数组中的一项。我写了这段代码,但是有些东西丢失了,我无法弄明白,因为圆圈阵列的所有项目都会在第一次点击时从舞台上消失。你能帮我吗?
var circles:Array = [circle1, circle2, circle3, circle4,circle5, circle6];
var counter:int = 0;
var square:Array = new Array(square1,square2,square3,square4,square5,square6,square7,square8,square9,square10,square11,square12,square13,square14,square15,square16,square17,square18,square19,square20,square21);
for(var i:int = 0; i < square.length; i++)
{
square[i].addEventListener(MouseEvent.CLICK, clickTomove);
function clickTomove(e:MouseEvent):void
{
trace("square");
var len:int=circles.length;
for(var o:int=0; o<circles.length; o++)
this.removeChild(circles[o]);
circles.splice(o,1);
}
}
答案 0 :(得分:1)
看起来你正试图做这样的事情:
for each (var square:MovieClip in squares) {
square.addEventListener(MouseEvent.CLICK, clickTomove);
}
function clickTomove(e:MouseEvent):void {
if (circles.length > 0) {
var circle:MovieClip = circles.pop();
removeChild(circle);
}
}
pop()
删除并返回圆圈数组中的最后一项。