我“认为”我所做的是正确的,但绝不可以。想象一下浮在场景中间的物体,我们可以称之为gbl.obj
。随机地将其他元素添加到场景中,如果gbl.obj
击中一个,则游戏结束。之后,如果你愿意,我想“重置”。重置包括删除添加的元素。我不知道引用这些对象的最佳方式,我愿意打赌这是我的问题。我这样添加它们:
var bottom = randomRange(75,250);
var pipeTop:Shape = new Shape; // initializing the variable named rectangle
pipeTop.graphics.beginFill(0xFF0000); // choosing the colour for the fill, here it is red
pipeTop.graphics.drawRect(0, 0, 75,bottom); // (x spacing, y spacing, width, height)
pipeTop.graphics.endFill(); // not always needed but I like to put it in to end the fill
pipeTop.addEventListener(Event.ENTER_FRAME, hitTarget);
pipeTop.name = 'pipe';
addChild(pipeTop);
var pipeBottom:Shape = new Shape; // initializing the variable named rectangle
pipeBottom.graphics.beginFill(0x00FF00); // choosing the colour for the fill, here it is red
pipeBottom.graphics.drawRect(0, bottom+125, 75,480-177-bottom); // (x spacing, y spacing, width, height)
pipeBottom.graphics.endFill(); // not always needed but I like to put it in to end the fill
pipeBottom.addEventListener(Event.ENTER_FRAME, hitTarget);
pipeBottom.name = 'pipe';
addChild(pipeBottom);
var topPipe = new Tween(pipeTop, 'x', None.easeIn,400,pipeTop.x-1000,200,false);
var bottomPipe = new Tween(pipeBottom, 'x', None.easeIn,400,pipeBottom.x-1000,200,false);
function hitTarget(e:Event):void{
if (gbl.obj.hitTestObject(pipeTop) == true || gbl.obj.hitTestObject(pipeBottom) == true) {
topPipe.stop();
bottomPipe.stop();
pipeTop.removeEventListener(Event.ENTER_FRAME, hitTarget);
pipeBottom.removeEventListener(Event.ENTER_FRAME, hitTarget);
fail();
}
}
基本上这些项目会在屏幕上移动。当被击中时,我停止它们可以调用一个基本上停止所有动画的失败函数。然后当按下重置按钮时,会发生这种情况:
Object(this).Overlay.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
startGround();
gbl.obj.y = 200;
gbl.timer = setInterval(doPipe,2300);
fall();
gbl.gameOver = false;
for(var i = 0; i < this.numChildren; i++){
var item = this.getChildAt(i);
if(item.name == 'pipe'){
this.removeChildAt(i);
}
}
现在每次都会发生什么,除了我的对象没有击中的那个之外,所有这些管道都被移除了。当我试图只追踪所有作为场景孩子的物体时,它就不存在了。当我删除任何检查并删除所有孩子时,它会保留。元素去了哪里?
答案 0 :(得分:0)
清理舞台的最快方法是将临时对象放在自己的容器中。虽然迭代子元素的方法应该有效,但使用可移动容器可以使清理成为一站式命令。
var bottom = randomRange(75,250);
var container:Sprite;
var pipeBottom:Shape, pipeTop:Shape;
var topPipe, bottomPipe;
function setup():void {
container = new Sprite();
addChild(container);
pipeTop = new Shape; // initializing the variable named rectangle
pipeTop.graphics.beginFill(0xFF0000); // choosing the colour for the fill, here it is red
pipeTop.graphics.drawRect(0, 0, 75,bottom); // (x spacing, y spacing, width, height)
pipeTop.graphics.endFill(); // not always needed but I like to put it in to end the fill
pipeTop.addEventListener(Event.ENTER_FRAME, hitTarget);
pipeTop.name = 'pipe';
container.addChild(pipeTop);
pipeBottom = new Shape; // initializing the variable named rectangle
pipeBottom.graphics.beginFill(0x00FF00); // choosing the colour for the fill, here it is red
pipeBottom.graphics.drawRect(0, bottom+125, 75,480-177-bottom); // (x spacing, y spacing, width, height)
pipeBottom.graphics.endFill(); // not always needed but I like to put it in to end the fill
pipeBottom.addEventListener(Event.ENTER_FRAME, hitTarget);
pipeBottom.name = 'pipe';
container.container.addChild(pipeBottom);
topPipe = new Tween(pipeTop, 'x', None.easeIn,400,pipeTop.x-1000,200,false);
bottomPipe = new Tween(pipeBottom, 'x', None.easeIn,400,pipeBottom.x-1000,200,false);
}
function hitTarget(e:Event):void{
if (gbl.obj.hitTestObject(pipeTop) == true || gbl.obj.hitTestObject(pipeBottom) == true) {
topPipe.stop();
bottomPipe.stop();
pipeTop.removeEventListener(Event.ENTER_FRAME, hitTarget);
pipeBottom.removeEventListener(Event.ENTER_FRAME, hitTarget);
fail();
}
}
function reset():void {
removeChild(container);
setup();
}