-2

I'm doing collision but when I press the key to move it crashes and says that my isBlocked boolean has something wrong and I cannot figure it out, heres the class:

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

I have been staring at it for a while and thought someone on this website must have the answer, if you do It would be nice thanks.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Genthorn
  • 21
  • 8

1 Answers1

0

Since there is no clear evidence or statement of where the null pointer came from, I can only give you my best guess. After reading your code, the isBlocked(float x, float y)caught my attention. You are using an integer to divide another integer. This will emit everything after the decimal point, that may cause the inaccuracy of the array index, which may lead to a null pointer.

Anton
  • 559
  • 2
  • 15