Java Tile Collision不工作?

时间:2014-06-10 21:57:14

标签: 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 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];
    }
}

我已经盯着它看了一会儿,并认为这个网站上的某个人必须有答案,如果你这样做,那将是非常感谢。

1 个答案:

答案 0 :(得分:0)

由于没有明确的证据或陈述空指针的来源,我只能给你最好的猜测。阅读完代码后,isBlocked(float x, float y)引起了我的注意。您正在使用整数来划分另一个整数。这将发出小数点后的所有内容,这可能会导致数组索引的不准确,这可能会导致空指针。