Java磁贴冲突不工作?
我正在进行碰撞,但当我按下移动键时,它会崩溃,并说我的isBlocked布尔值出错,我无法找出原因,下面是类:Java磁贴冲突不工作?,java,collision-detection,slick2d,Java,Collision Detection,Slick2d,我正在进行碰撞,但当我按下移动键时,它会崩溃,并说我的isBlocked布尔值出错,我无法找出原因,下面是类: public class Map_Test { private boolean[][] blocked; private static final int SIZE = 32; TiledMap map_test = null; int PlayerX; int PlayerY; public void init() throws
public class Map_Test {
private boolean[][] blocked;
private static final int SIZE = 32;
TiledMap map_test = null;
int PlayerX;
int PlayerY;
public void init() throws SlickException{
try {
map_test = new TiledMap("res/untitled.tmx");
} catch (SlickException e) {
e.printStackTrace();
}
blocked = new boolean[map_test.getWidth()][map_test.getHeight()];
for (int xAxis=0;xAxis<map_test.getWidth(); xAxis++)
{
for (int yAxis=0;yAxis<map_test.getHeight(); yAxis++)
{
int tileID = map_test.getTileId(xAxis, yAxis, 0);
String value = map_test.getTileProperty(tileID, "blocked", "false");
if ("true".equals(value))
{
blocked[xAxis][yAxis] = true;
}
}
}
}
public void update(GameContainer gc, int delta) throws SlickException {
//PlayerX = Player.X;
//PlayerY = Player.Y;
Input i = gc.getInput();
if (i.isKeyDown(Input.KEY_W)) {
//if (!isBlocked(Player.X, Player.Y - delta * 0.1f)) {
Player.X+=Player.speed *delta;
//}
}
if (i.isKeyDown(Input.KEY_S)) {
//if (!isBlocked(Player.X, Player.Y + SIZE + delta * 0.1f)) {
Player.X+=Player.speed *delta;
//}
}
if (i.isKeyDown(Input.KEY_A)) {
//if (!isBlocked(Player.X - delta * 0.1f, Player.Y)) {
Player.X+=Player.speed *delta;
//}
}
if (i.isKeyDown(Input.KEY_D)) {
//if (!isBlocked(Player.X + SIZE + delta * 0.1f, Player.Y)) {
Player.X+=Player.speed *delta;
//}
}
}
public void render(Graphics g) throws SlickException{
map_test.render(0, 0);
}
public boolean isBlocked(float x, float y)
{
int xBlock = (int)x / SIZE;
int yBlock = (int)y / SIZE;
return blocked[xBlock][yBlock];
}
}
公共类映射\u测试{
私有布尔值[][]被阻止;
私有静态最终整数大小=32;
TiledMap\u test=null;
int-PlayerX;
智力游戏;
public void init()引发异常{
试一试{
map_test=新的tileMap(“res/untitled.tmx”);
}接住(滑溜){
e、 printStackTrace();
}
blocked=新布尔值[map_test.getWidth()][map_test.getHeight()];
对于(int-xAxis=0;xAxis,由于没有明确的证据或陈述说明空指针来自何处,我只能给您最好的猜测。在阅读您的代码后,被阻止(float x,float y)
引起了我的注意。您正在使用一个整数除以另一个整数。这将发出小数点后的所有内容,这可能会导致数组索引不准确,这可能会导致空指针。而且他们没有检查平铺贴图的边界。