這是地圖: 藍色方形圖層名稱:背景 青色廣場圖層名稱:對象 白方=玩家 http://prntscr.com/2c2m5l(心中永遠的黑色空間)。Java的Slick2D瓷磚地圖碰撞失敗
看一對夫婦的教程後,我結束了編碼這個類:
public class LevelOne extends BasicGameState{
public int state;
boolean esc;
Input input;
private TiledMap map;
private int x,y;
public LevelOne(int state){
this.state = state;
}
@Override
public void init(GameContainer arg0, StateBasedGame arg1)
throws SlickException {
esc = false;
map = new TiledMap("res/map/map.tmx");
x = 1;
y = 1;
}
@Override
public void render(GameContainer arg0, StateBasedGame arg1, Graphics g)
throws SlickException {
map.render(0, 0);
g.fillRect(x * 32, y * 32, 32, 32);
}
@Override
public void update(GameContainer gc, StateBasedGame sbg, int arg2)
throws SlickException {
input = gc.getInput();
if(esc){
Mouse.setGrabbed(false);
}else{
Mouse.setGrabbed(true);
}
if(input.isKeyPressed(Input.KEY_ESCAPE)){
esc = true;
}
if(esc){
Mouse.setGrabbed(false);
if(input.isKeyPressed(Input.KEY_R)){
esc = false;
}else if(input.isKeyPressed(Keyboard.KEY_Q)){
System.exit(0);
}else if(input.isKeyPressed(Keyboard.KEY_M)){
esc = false;
sbg.enterState(0);
Mouse.setGrabbed(false);
}
}
int objLayer = map.getLayerIndex("Objects");
map.getTileId(0, 0, objLayer);
if(input.isKeyPressed(Input.KEY_RIGHT)){
if(map.getTileId(x+1, y, objLayer) == 0){
x++;
}
}
if(input.isKeyPressed(Input.KEY_LEFT)){
if(map.getTileId(x-1, y, objLayer) == 0){
x--;
}
}
if(input.isKeyPressed(Input.KEY_UP)){
if(map.getTileId(x, y-1, objLayer) == 0){
y--;
}
}
if(input.isKeyPressed(Input.KEY_DOWN)){
if(map.getTileId(x, y+1, objLayer) == 0){
y++;
}
}
}
@Override
public int getID() {
return state;
}
}
碰撞檢測不工作,我不明白爲什麼我需要這兩條線:
int objLayer = map.getLayerIndex("Objects");
map.getTileId(0, 0, objLayer);//this one in particular