2d侧卷轴上的碰撞检测:未正确碰撞

时间:2013-04-24 00:46:11

标签: java collision-detection

我的碰撞方法存在问题。问题是游戏中有两个敌人。它与循环中的一个敌人相交,并继续返回true以进行碰撞。但是如果在这个数组中有第二个敌人List它将不会与第二个对象发生碰撞,因此导致它返回false并且玩家继续行走。任何关于如何在他与任何敌人接触时让他停下来的想法,而不仅仅是继续,因为他不与所有敌人接触? 谢谢,这是代码。

public void checkCollision(){
    ArrayList<Enemy> enemy = c.getEnemyList();
    for ( int i = 0; i < enemy.size(); i++){
        Enemy e = enemy.get(i);

        if (!getBounds().intersects(e.getBounds())){
            walk();
            return;
        }
        if (getBounds().intersects(e.getBounds())){
            if (e.getHP() <= 0){
                c.removeEnemy(e);
                walk();
                return;
            }
            fight();
            if (count == 25 || count == 65){
                int dd = DCalc.calcDmg(atk, atkMAX);
                e.dmg(dd);
            }

    }
    }

}

1 个答案:

答案 0 :(得分:0)

这只是“早期回归”问题的另一个例子。当您需要检查格式(如果ANY,x,否则为y)或(如果是ALL,x,否则为y)并以格式(如果为FIRST,x,则为y)对其进行重新格式化时,会出现此问题。 / p>

要解决此问题,您需要按如下方式重新制作算法:

bool collided = false
For each enemy:
    Are we colliding with this enemy?
    If we are, do collision detection and set `collided` to true
end for

If `collided` is false, NOW we can run the code that should only run if we collided with nothing