2014-06-10 2 views
-2

나는 충돌을하고 있어요하지만 이동 키를 누를 때 충돌하고,을 heres 클래스 내 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]; 
    } 
} 

을 잠시 동안이 웹 사이트의 누군가가 대답을해야한다고 생각합니다.한다면 감사합니다.

+0

디버거를 사용하십시오. – redFIVE

답변

0

null 포인터가 어디서 왔는지에 대한 명확한 증거 또는 진술이 없으므로 나는 최선의 추측을 할 수 있습니다. 코드를 읽은 후 isBlocked(float x, float y)이 내주의를 끌었습니다. 다른 정수를 나눌 정수를 사용하고 있습니다. 이것은 배열 인덱스의 부정확성을 초래할 수있는 소수점 이후의 모든 것을 방출 할 것이고, 이것은 널 포인터가 될 수 있습니다.

+0

타일 맵의 경계를 확인하지 않습니다. – Samich

관련 문제