2015-10-19 57 views
0

我正在製作一款採礦遊戲,如果你點擊某個地方,你會刪除鋪設的地塊。現在所有的塊都只是正方形,並且只有在我的2d布爾數組中的位置爲真時才被繪製。所以,我想採取這樣的立場,並將其設置爲false,無論你點擊這裏是我的inputprocessor輸入不能抓取正確的座標libgdx java

private Vector2 tmp = new Vector2(); 
@Override 
public boolean touchDown(int screenX, int screenY, int pointer, int button) { 
    tmp.x = screenX/MapGrid.CELL_SIZE; 
    tmp.y = screenY/MapGrid.CELL_SIZE; 
    cam.unproject(new Vector3(tmp.x, tmp.y, 0)); 
    grid.manipulateGrid((int)(tmp.x), (int)(tmp.y), false); 
    System.out.println("Clicked at: (" + tmp.x + ", " + tmp.y +")"); 
    return false; 
} 

的着陸方法,我也有翻譯相機我的球員的位置。 grid.manipulateGrid需要一個x和一個y並將其設置爲false。我的球員位於網格座標(10,126),當我點擊他旁邊時,它說我點擊(35,24)我不確定我是否正在做這件事,但我一直在尋找到處,並且可以找不到解決方案。我發現類似的問題,但沒有結果。如果有人能告訴我如何將點擊調整到玩家的座標,我會感到難以置信的欣賞。

回答

2

你有unprojecting它

@Override 
public boolean touchDragged(int screenX, int screenY, int pointer) { 
    tmp.x = screenX; 
    tmp.y = (Gdx.graphics.getHeight() - screenY); 
    tmp.z = 0; 
    cam.unproject(tmp); 
    grid.manipulateGrid((int)(tmp.x)/MapGrid.CELL_SIZE, (int)(tmp.y)/MapGrid.CELL_SIZE, false); 
    System.out.println("Clicked at: (" + tmp.x/MapGrid.CELL_SIZE + ", " + tmp.y/MapGrid.CELL_SIZE +")"); 
    return false; 
} 
+0

謝謝!!!!這工作完美 – Luke

2

那麼繪圖的座標與輸入不一樣。 LibGDX中的輸入座標從左上角開始爲(0,0),以及您繪製的一般世界的座標以及所有開始向下的左邊的座標。所以,你需要做的是這樣的:

@Override 
public boolean touchDown(int screenX, int screenY, int pointer, int button) { 
    tmp.x = screenX/MapGrid.CELL_SIZE; 
    tmp.y = (Gdx.graphics.getHeight() - screenY)/ MapGrid.CELL_SIZE; 
    cam.unproject(new Vector3(tmp.x, tmp.y, 0)); 
    grid.manipulateGrid((int)(tmp.x), (int)(tmp.y), false); 
    System.out.println("Clicked at: (" + tmp.x + ", " + tmp.y +")"); 
    return false; 
} 

欲瞭解更多信息,請閱讀此: https://gamedev.stackexchange.com/questions/103145/libgdx-input-y-axis-flipped

+0

是後劃分mapgrid但這並沒有解決問題 – Luke

+0

您是否打印過MapGrid.CELL_SIZE變量?你確定它是正確的嗎? –

+0

我100%確定它是正確的,我只是打印它,它是 – Luke