我定义了这个简单的方法:
public static boolean isBorder(int x, int y) throws CollisionDetectionException {
try {
if ( (levelItems[x][y] instanceof StaticGameObject && levelItems[x][y].isVisible()) ||
(levelItems[x-1][y] instanceof StaticGameObject && levelItems[x-1][y].isVisible()) ||
(levelItems[x][y+1] instanceof StaticGameObject && levelItems[x][y+1].isVisible()) ||
(levelItems[x][y-1] instanceof StaticGameObject && levelItems[x][y-1].isVisible()) ||
(levelItems[x-1][y-1] instanceof StaticGameObject && levelItems[x-1][y-1].isVisible()) ||
(levelItems[x-1][y+1] instanceof StaticGameObject &&levelItems[x-1][y+1].isVisible()) ||
(levelItems[x+1][y] instanceof StaticGameObject && levelItems[x+1][y].isVisible()) ||
(levelItems[x+1][y+1] instanceof StaticGameObject && levelItems[x+1][y+1].isVisible()) ||
(levelItems[x+1][y-1] instanceof StaticGameObject && levelItems[x+1][y-1].isVisible()) ) {
return true;
} else {
return false;
}
} catch (ArrayIndexOutOfBoundsException e) {
throw new CollisionDetectionException("Collision couldn't be checked because checking position " + x + "/" + y + " caluclated values below (0/0)");
}
}
如您所见,我有一个二维数组。现在我想要检查特定的位置((x/y) ->方法参数),如果二维数组的相邻区域中存在任何可见的StaticGameObject。
levelItems数组由所谓的GameObject组成。StaticGameObject是GameObject的直接子类。
有什么提示可以改进这个方法吗?