网格libgdx中的屏幕坐标

时间:2015-04-28 20:24:37

标签: java android libgdx

我使用ShapeRenderer.line创建了一个网格。它绘制了一个10x10网格。

private void setupCamera() {
        // TODO Auto-generated method stub
        camera = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
        camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0f);
        camera.update();
    }

@Override
public void draw() {
     for(int x = 0; x < rows; x++){
         for(int y = 0; y < columns; y++){

                 shapeRenderer.line(x+1, 12-y, x+2, 12-y);
                 shapeRenderer.line(x+1, 12-y, x+1, 11-y);
                 shapeRenderer.line(x+2, 12-y, x+2, 11-y);
                 shapeRenderer.line(x+1, 11-y, x+2, 11-y);
         }
     }
  }

public class Cell 
{
   private static int gridWidth = 1;
   private static int gridHeight = 1;

   public int coordinatesX;
   public int coordinatesY;

   public Cell(){}

   public Cell(int x, int y){

     this.coordinatesX = x;
     this.coordinatesY = y;
   }

   public void drawLine(ShapeRenderer shapeRenderer){

     int left = coordinatesX * gridWidth;
     int right = left + gridWidth;
     int bottom = coordinatesY * gridHeight;
     int top= bottom + gridHeight;

     shapeRenderer.line(left , top, left, bottom);
     shapeRenderer.line(right , top, right , bottom);
     shapeRenderer.line(left , top, right , top);
     shapeRenderer.line(left , bottom, right , bottom);
   }

   public void drawImageStartPosition(SpriteBatch spritebatch, int x, int y, Texture texture){

    int left = x * texture.getWidth();
    int bottom = y * texture.getHeight(); 

    spritebatch.draw(texture, left, bottom, texture.getWidth(), texture.getHeight());
}

}

我创建了一个纹理(ball.png),我想要完成的是我想根据我创建的网格将该纹理放在一个单元格内(使用该特定单元格的屏幕坐标)。

任何人都可以指点我应该怎么做?

1 个答案:

答案 0 :(得分:2)

尝试将每个网格单元格转换为对象。

public class GridSquare {

    private static int gridWidth = 10;
    private static int gridHeight = 10;

    public final int xPos;
    public final int yPos;

    public GridSquare(int x, int y){
        xPos = x;
        yPos = y;
    }

    public void drawOutLine(ShapeRenderer shapeRenderer){

         /*
         If the gridWidth and gridHeight don't change over time then you can
         move the calculation of the left, right, bottom and top positions 
         into the constructor for better performance.
         */
         int left = xPos * gridWidth;
         int right = left + gridWidth;
         int bottom = yPos * gridHeight;
         int top= bottom + gridHeight;

         shapeRenderer.line(left , top, left, bottom);
         shapeRenderer.line(right , top, right , bottom);
         shapeRenderer.line(left , top, right , top);
         shapeRenderer.line(left , bottom, right , bottom);
    }

    public void drawImage(Spritebatch spritebatch, Texture texture){

         /*
         If the gridWidth and gridHeight don't change over time then you can
         move the calculation of the left and bottom positions 
         into the constructor for better performance.
         */

        int left = xPos * gridWidth;
        int bottom = yPos * gridHeight; 

        spritebatch.draw(
                texture,
                left,
                bottom,
                gridWidth,
                gridHeight
        );
    }
}

然后你可以迭代它们并告诉它们渲染自己,他们就会知道如何去做。

 shapeRenderer.begin();
 for(int x = 0; x < rows; x++){
     for(int y = 0; y < columns; y++){
         gridSquares[x][y].drawOutLine(shapeRenderer);
     }
 }
 shapeRenderer.end();


 spritebatch.begin();
 for(int x = 0; x < rows; x++){
     for(int y = 0; y < columns; y++){
         // Assuming you have loaded the png image into a variable named balltexture
         gridSquares[x][y].drawImage(spritebatch, balltexture);
     }
 }
 spritebatch.end();