【发布时间】:2020-09-23 21:07:17
【问题描述】:
我正在尝试围绕网格移动对象并计算战斗(当它们接触时),但每次我得到一个交互时,它们都会计算两次。我明白为什么,但想不出办法。
代码:
// Move all the objects
for(int i = 0; i < objectArray.length; i++) {
objectArray[i].move();
}
// Check to see if any of the objectsare touching
for(int i = 0; i < objectArray.length; i++) {
for(int j = 0; j < objectArray.length; j++) {
if(objectArray[i] != objectArray[j]) {
if(objectArray[i].getX() == objectArray[j].getX() && objectArray[i].getY() == objectArray[j].getY()) {
System.out.println("FIGHT");
System.out.println("There is a fight between " + objectArray[i].getName() + " and " + objectArray[j].getName() + "." );
fights++;
}
}
}
}
}
【问题讨论】: