Actionscript 3 动作脚本3:添加到场景中的项目,一旦命中,就无法删除/找到要删除的项目

Actionscript 3 动作脚本3:添加到场景中的项目,一旦命中,就无法删除/找到要删除的项目,actionscript-3,target,Actionscript 3,Target,我认为我所做的是正确的,但决不能是正确的。想象一个漂浮在场景中间的物体,我们可以称之为GBL.Obj.随机添加其他元素到场景中,如果gbl.obj命中一个,游戏结束。现在,如果你愿意,我想重置。重置包括删除已添加的元素。我不知道引用这些对象的最佳方式,我敢打赌这是我的问题。我这样加上: var bottom = randomRange(75,250); var pipeTop:Shape = new Shape; // initializing the variable named rectan

我认为我所做的是正确的,但决不能是正确的。想象一个漂浮在场景中间的物体,我们可以称之为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();
  }
}
基本上,这些项目在屏幕上移动。当点击时,我可以调用一个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);
  }
}

现在每次都会发生的事情是所有这些管道都被移除,除了我的物体没有击中的那个管道。当我试图追踪场景中的所有子对象时,它不在那里。当我删除任何检查并删除所有子项时,它将保持不变。元素到哪里去了?

清理舞台的最快方法是将临时对象放在它们自己的容器中。虽然在子元素上迭代的方法应该可以工作,但使用可移除的容器会使清理成为一个一站式命令

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();
}

用你的方法,我一次只能在舞台上放一根上下水管,对吗?我想我可以将容器对象传递给hitTarget函数,而不是查看全局对象。您可以拥有任意数量的管道。任何实例化的对象都有一个默认的唯一可识别名称,即instance355,从实用的角度来看,子元素只是数字索引数组DisplayListContainer的属性。您共享的示例仅创建了2个管道。如果您想要更多,为什么不抽象该部分,并使用循环生成X个管道。