2013-04-24 96 views
0

我遇到下面的碰撞方法有问题。问题是游戏中有两个敌人。它与循环内的一个敌人相交,并继续返回true以实现碰撞。但是,如果这个数组中有第二个敌人,它将不会与第二个对象发生冲突,因此导致它返回false并且玩家继续走。任何关于如何让他在与任何敌人接触时停止的想法,而不是因为他没有接触到所有敌人而继续下去? 谢谢,这里是代码。2d侧面滚动条上的碰撞检测:没有正确碰撞

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

    } 
    } 

} 
+0

建议:这个移至[游戏开发网站(http://gamedev.stackexchange.com/) – 2013-04-24 00:48:05

+0

不了解该网站,谢谢D先生通知我。 – 2013-04-24 00:51:57

+0

希望我可以自己提出这个问题,但没有足够的声望。如果你提出这个问题,主持人会为你做。 – 2013-04-24 00:53:22

回答

0

这只是'早退'问题的另一个例子。如果需要检查格式(如果ANY,x,else y)或(如果ALL,x,否则y),并且以格式(如果FIRST,x,else y)重新表示它,则会出现此问题。

为了解决这个问题,就需要重拍算法如下:

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 
+0

好吧,我现在得到它,非常感谢。其工作= D – 2013-04-24 01:02:35