AS3 hitTestObject未正确注册



我想通过点击attakButton并使用hitTestObject来击中我的目标3次,它的寿命也将从3减少到0,然后在击中我的靶后,如果寿命为0,它将进入祝贺屏幕或关键帧。我的问题是,它只记录我第一次击中目标时的情况,然后在我第二次、第三次击中目标后什么都没发生。。。等等。请帮忙?

    var life = 3;
    attackButton.addEventListener (MouseEvent.CLICK, attack01);
    function attack01 (e:MouseEvent): void {
        colliderPlayer.gotoAndPlay(2);
    } 
    stage.addEventListener(Event.ENTER_FRAME, lifeEnemy);
    function lifeEnemy(evt:Event): void {
        if(this.colliderPlayer.hitTestObject(boss)){
            stage.removeEventListener(Event.ENTER_FRAME, lifeEnemy);
            life = life - 1;
            trace(lifeEnemy);
            if(lifeEnemy==0) {
                MovieClip(root).gotoAndStop('ending');
            }
        }
    }

命中测试停止运行的原因是,如果命中测试成功,您将删除侦听器,以便下次检查命中测试。我指的是:

stage.removeEventListener(Event.ENTER_FRAME, lifeEnemy);

如果你想在敌人的生命值为0时停止检查命中率,请执行以下操作:

var life = 3;
attackButton.addEventListener (MouseEvent.CLICK, attack01);
function attack01 (e:MouseEvent): void {
    colliderPlayer.gotoAndPlay(2);
} 
stage.addEventListener(Event.ENTER_FRAME, lifeEnemy);
function lifeEnemy(evt:Event): void {
    if(this.colliderPlayer.hitTestObject(boss)){
        life = life - 1;
        trace(life);
        if(life==0) {
            stage.removeEventListener(Event.ENTER_FRAME, lifeEnemy);
            MovieClip(root).gotoAndStop('ending');
        }
    }
}

最新更新