2014-12-08 52 views
0

我已經創建了一個非常基本的瓷磚系統,我希望能夠獲得鼠標懸停在當前的瓷磚,這對於第一個瓷磚創建,但除此之外,我什麼也沒有看。循環通過瓷磚,只能得到可以得到第一個瓷磚的索引

下面是設置和繪製圖塊的代碼以及「Currently hovering」文本。

private static final int TILE_WIDTH = 64; 
private static final int TILE_HEIGHT = 64; 

private static Tile[][] gridTiles = new Tile[(int)Math.ceil(Gdx.graphics.getHeight()/TILE_HEIGHT)][(int)Math.ceil(Gdx.graphics.getWidth()/TILE_WIDTH)]; 

public CombatGrid() {} // Cannot instantiate this class 

static {   
    // Rows 
    for(int r = 0; r < gridTiles.length; r++) { 
     // Columns 
     for(int c = 0; c < gridTiles[0].length; c++) { 
      gridTiles[r][c] = new Tile((c * TILE_WIDTH), (r * TILE_HEIGHT), TILE_WIDTH, TILE_HEIGHT); 
      System.out.println("New tile("+r+","+c+") was created at: x: " + (c * TILE_WIDTH) + " y: " + (r * TILE_HEIGHT)); 
     } 
    } 
} 

public static void draw(Batch batch) { 
    for(int r = 0; r < gridTiles.length; r++) { 
     for(int c = 0; c < gridTiles[0].length; c++) { 
      gridTiles[r][c].draw(batch); 
      if(gridTiles[r][c].hovering(Gdx.input.getX(), Gdx.input.getY())) { 
       Assets.defaultFont.draw(batch, "Current hover index("+r+","+c+")", Client.TOP_LEFT_X + 10, Client.TOP_LEFT_Y - 30); 
      } 
     } 
    } 
} 

文本僅當我do sth對瓷磚上顯示[0] [0]

下面是來自瓷磚類

public boolean hovering(float x, float y) { 
    if((x > this.x) && (x < this.width)) { 
     if((y > this.y) && (y < this.height)) { 
      return true; 
     } 
    } 
    return false; 
} 

鼠標輸入我是懸停方法傳遞到盤旋是正確的,當我移出0,0瓦時,它不再被繪製。

而且,我調試我的鼠標位置

Assets.defaultFont.draw(batch, "Curret mouse X: " + Gdx.input.getX() + " Y: " + Gdx.input.getY(), Client.TOP_LEFT_X + 10, Client.TOP_LEFT_Y - 10); 

它表明,我已經設置了數學是正確的,或者至少應該是,除非我錯過了什麼

現在,我知道所有的瓷磚被greated和靜態初始化這說明正確放置因爲Debugging Statement的,:

New tile(0,0) was created at: x: 0 y: 0 
New tile(0,1) was created at: x: 64 y: 0 
New tile(0,2) was created at: x: 128 y: 0 
New tile(0,3) was created at: x: 192 y: 0 
New tile(0,4) was created at: x: 256 y: 0 
New tile(0,5) was created at: x: 320 y: 0 
New tile(0,6) was created at: x: 384 y: 0 
New tile(0,7) was created at: x: 448 y: 0 
New tile(0,8) was created at: x: 512 y: 0 
New tile(0,9) was created at: x: 576 y: 0 
New tile(0,10) was created at: x: 640 y: 0 
New tile(0,11) was created at: x: 704 y: 0 
New tile(0,12) was created at: x: 768 y: 0 
New tile(0,13) was created at: x: 832 y: 0 
New tile(0,14) was created at: x: 896 y: 0 
New tile(0,15) was created at: x: 960 y: 0 
New tile(0,16) was created at: x: 1024 y: 0 
New tile(0,17) was created at: x: 1088 y: 0 
New tile(1,0) was created at: x: 0 y: 64 
New tile(1,1) was created at: x: 64 y: 64 
New tile(1,2) was created at: x: 128 y: 64 
New tile(1,3) was created at: x: 192 y: 64 
New tile(1,4) was created at: x: 256 y: 64 
New tile(1,5) was created at: x: 320 y: 64 
New tile(1,6) was created at: x: 384 y: 64 
New tile(1,7) was created at: x: 448 y: 64 
New tile(1,8) was created at: x: 512 y: 64 
New tile(1,9) was created at: x: 576 y: 64 
New tile(1,10) was created at: x: 640 y: 64 
New tile(1,11) was created at: x: 704 y: 64 
New tile(1,12) was created at: x: 768 y: 64 
New tile(1,13) was created at: x: 832 y: 64 
New tile(1,14) was created at: x: 896 y: 64 
New tile(1,15) was created at: x: 960 y: 64 
New tile(1,16) was created at: x: 1024 y: 64 
New tile(1,17) was created at: x: 1088 y: 64 
New tile(2,0) was created at: x: 0 y: 128 
New tile(2,1) was created at: x: 64 y: 128 
New tile(2,2) was created at: x: 128 y: 128 
New tile(2,3) was created at: x: 192 y: 128 
New tile(2,4) was created at: x: 256 y: 128 
New tile(2,5) was created at: x: 320 y: 128 
New tile(2,6) was created at: x: 384 y: 128 
New tile(2,7) was created at: x: 448 y: 128 
New tile(2,8) was created at: x: 512 y: 128 
New tile(2,9) was created at: x: 576 y: 128 
New tile(2,10) was created at: x: 640 y: 128 
New tile(2,11) was created at: x: 704 y: 128 
New tile(2,12) was created at: x: 768 y: 128 
New tile(2,13) was created at: x: 832 y: 128 
New tile(2,14) was created at: x: 896 y: 128 
New tile(2,15) was created at: x: 960 y: 128 
New tile(2,16) was created at: x: 1024 y: 128 
New tile(2,17) was created at: x: 1088 y: 128 
New tile(3,0) was created at: x: 0 y: 192 
New tile(3,1) was created at: x: 64 y: 192 
New tile(3,2) was created at: x: 128 y: 192 
New tile(3,3) was created at: x: 192 y: 192 
New tile(3,4) was created at: x: 256 y: 192 
New tile(3,5) was created at: x: 320 y: 192 
New tile(3,6) was created at: x: 384 y: 192 
New tile(3,7) was created at: x: 448 y: 192 
New tile(3,8) was created at: x: 512 y: 192 
New tile(3,9) was created at: x: 576 y: 192 
New tile(3,10) was created at: x: 640 y: 192 
New tile(3,11) was created at: x: 704 y: 192 
New tile(3,12) was created at: x: 768 y: 192 
New tile(3,13) was created at: x: 832 y: 192 
New tile(3,14) was created at: x: 896 y: 192 
New tile(3,15) was created at: x: 960 y: 192 
New tile(3,16) was created at: x: 1024 y: 192 
New tile(3,17) was created at: x: 1088 y: 192 
New tile(4,0) was created at: x: 0 y: 256 
New tile(4,1) was created at: x: 64 y: 256 
New tile(4,2) was created at: x: 128 y: 256 
New tile(4,3) was created at: x: 192 y: 256 
New tile(4,4) was created at: x: 256 y: 256 
New tile(4,5) was created at: x: 320 y: 256 
New tile(4,6) was created at: x: 384 y: 256 
New tile(4,7) was created at: x: 448 y: 256 
New tile(4,8) was created at: x: 512 y: 256 
New tile(4,9) was created at: x: 576 y: 256 
New tile(4,10) was created at: x: 640 y: 256 
New tile(4,11) was created at: x: 704 y: 256 
New tile(4,12) was created at: x: 768 y: 256 
New tile(4,13) was created at: x: 832 y: 256 
New tile(4,14) was created at: x: 896 y: 256 
New tile(4,15) was created at: x: 960 y: 256 
New tile(4,16) was created at: x: 1024 y: 256 
New tile(4,17) was created at: x: 1088 y: 256 
New tile(5,0) was created at: x: 0 y: 320 
New tile(5,1) was created at: x: 64 y: 320 
New tile(5,2) was created at: x: 128 y: 320 
New tile(5,3) was created at: x: 192 y: 320 
New tile(5,4) was created at: x: 256 y: 320 
New tile(5,5) was created at: x: 320 y: 320 
New tile(5,6) was created at: x: 384 y: 320 
New tile(5,7) was created at: x: 448 y: 320 
New tile(5,8) was created at: x: 512 y: 320 
New tile(5,9) was created at: x: 576 y: 320 
New tile(5,10) was created at: x: 640 y: 320 
New tile(5,11) was created at: x: 704 y: 320 
New tile(5,12) was created at: x: 768 y: 320 
New tile(5,13) was created at: x: 832 y: 320 
New tile(5,14) was created at: x: 896 y: 320 
New tile(5,15) was created at: x: 960 y: 320 
New tile(5,16) was created at: x: 1024 y: 320 
New tile(5,17) was created at: x: 1088 y: 320 
New tile(6,0) was created at: x: 0 y: 384 
New tile(6,1) was created at: x: 64 y: 384 
New tile(6,2) was created at: x: 128 y: 384 
New tile(6,3) was created at: x: 192 y: 384 
New tile(6,4) was created at: x: 256 y: 384 
New tile(6,5) was created at: x: 320 y: 384 
New tile(6,6) was created at: x: 384 y: 384 
New tile(6,7) was created at: x: 448 y: 384 
New tile(6,8) was created at: x: 512 y: 384 
New tile(6,9) was created at: x: 576 y: 384 
New tile(6,10) was created at: x: 640 y: 384 
New tile(6,11) was created at: x: 704 y: 384 
New tile(6,12) was created at: x: 768 y: 384 
New tile(6,13) was created at: x: 832 y: 384 
New tile(6,14) was created at: x: 896 y: 384 
New tile(6,15) was created at: x: 960 y: 384 
New tile(6,16) was created at: x: 1024 y: 384 
New tile(6,17) was created at: x: 1088 y: 384 
New tile(7,0) was created at: x: 0 y: 448 
New tile(7,1) was created at: x: 64 y: 448 
New tile(7,2) was created at: x: 128 y: 448 
New tile(7,3) was created at: x: 192 y: 448 
New tile(7,4) was created at: x: 256 y: 448 
New tile(7,5) was created at: x: 320 y: 448 
New tile(7,6) was created at: x: 384 y: 448 
New tile(7,7) was created at: x: 448 y: 448 
New tile(7,8) was created at: x: 512 y: 448 
New tile(7,9) was created at: x: 576 y: 448 
New tile(7,10) was created at: x: 640 y: 448 
New tile(7,11) was created at: x: 704 y: 448 
New tile(7,12) was created at: x: 768 y: 448 
New tile(7,13) was created at: x: 832 y: 448 
New tile(7,14) was created at: x: 896 y: 448 
New tile(7,15) was created at: x: 960 y: 448 
New tile(7,16) was created at: x: 1024 y: 448 
New tile(7,17) was created at: x: 1088 y: 448 
New tile(8,0) was created at: x: 0 y: 512 
New tile(8,1) was created at: x: 64 y: 512 
New tile(8,2) was created at: x: 128 y: 512 
New tile(8,3) was created at: x: 192 y: 512 
New tile(8,4) was created at: x: 256 y: 512 
New tile(8,5) was created at: x: 320 y: 512 
New tile(8,6) was created at: x: 384 y: 512 
New tile(8,7) was created at: x: 448 y: 512 
New tile(8,8) was created at: x: 512 y: 512 
New tile(8,9) was created at: x: 576 y: 512 
New tile(8,10) was created at: x: 640 y: 512 
New tile(8,11) was created at: x: 704 y: 512 
New tile(8,12) was created at: x: 768 y: 512 
New tile(8,13) was created at: x: 832 y: 512 
New tile(8,14) was created at: x: 896 y: 512 
New tile(8,15) was created at: x: 960 y: 512 
New tile(8,16) was created at: x: 1024 y: 512 
New tile(8,17) was created at: x: 1088 y: 512 
New tile(9,0) was created at: x: 0 y: 576 
New tile(9,1) was created at: x: 64 y: 576 
New tile(9,2) was created at: x: 128 y: 576 
New tile(9,3) was created at: x: 192 y: 576 
New tile(9,4) was created at: x: 256 y: 576 
New tile(9,5) was created at: x: 320 y: 576 
New tile(9,6) was created at: x: 384 y: 576 
New tile(9,7) was created at: x: 448 y: 576 
New tile(9,8) was created at: x: 512 y: 576 
New tile(9,9) was created at: x: 576 y: 576 
New tile(9,10) was created at: x: 640 y: 576 
New tile(9,11) was created at: x: 704 y: 576 
New tile(9,12) was created at: x: 768 y: 576 
New tile(9,13) was created at: x: 832 y: 576 
New tile(9,14) was created at: x: 896 y: 576 
New tile(9,15) was created at: x: 960 y: 576 
New tile(9,16) was created at: x: 1024 y: 576 
New tile(9,17) was created at: x: 1088 y: 576 
New tile(10,0) was created at: x: 0 y: 640 
New tile(10,1) was created at: x: 64 y: 640 
New tile(10,2) was created at: x: 128 y: 640 
New tile(10,3) was created at: x: 192 y: 640 
New tile(10,4) was created at: x: 256 y: 640 
New tile(10,5) was created at: x: 320 y: 640 
New tile(10,6) was created at: x: 384 y: 640 
New tile(10,7) was created at: x: 448 y: 640 
New tile(10,8) was created at: x: 512 y: 640 
New tile(10,9) was created at: x: 576 y: 640 
New tile(10,10) was created at: x: 640 y: 640 
New tile(10,11) was created at: x: 704 y: 640 
New tile(10,12) was created at: x: 768 y: 640 
New tile(10,13) was created at: x: 832 y: 640 
New tile(10,14) was created at: x: 896 y: 640 
New tile(10,15) was created at: x: 960 y: 640 
New tile(10,16) was created at: x: 1024 y: 640 
New tile(10,17) was created at: x: 1088 y: 640 
New tile(11,0) was created at: x: 0 y: 704 
New tile(11,1) was created at: x: 64 y: 704 
New tile(11,2) was created at: x: 128 y: 704 
New tile(11,3) was created at: x: 192 y: 704 
New tile(11,4) was created at: x: 256 y: 704 
New tile(11,5) was created at: x: 320 y: 704 
New tile(11,6) was created at: x: 384 y: 704 
New tile(11,7) was created at: x: 448 y: 704 
New tile(11,8) was created at: x: 512 y: 704 
New tile(11,9) was created at: x: 576 y: 704 
New tile(11,10) was created at: x: 640 y: 704 
New tile(11,11) was created at: x: 704 y: 704 
New tile(11,12) was created at: x: 768 y: 704 
New tile(11,13) was created at: x: 832 y: 704 
New tile(11,14) was created at: x: 896 y: 704 
New tile(11,15) was created at: x: 960 y: 704 
New tile(11,16) was created at: x: 1024 y: 704 
New tile(11,17) was created at: x: 1088 y: 704 
+0

考慮提供一個[可運行示例](https://stackoverflow.com/help/mcve),它演示了您的問題。這將導致更少的困惑和更好的迴應 – MadProgrammer 2014-12-08 01:32:14

回答

1

試試這個;

public boolean hovering(float x, float y) { 
    if((x > this.x) && (x < this.x + this.width)) { 
     if((y > this.y) && (y < this.y + this.height)) { 
      return true; 
     } 
    } 
    return false; 
} 
+0

哦,哇,我怎麼錯過了。 – Hobbyist 2014-12-08 01:37:08