网格内的屏幕坐标 libgdx

screen coordinates inside a grid 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),我想要完成的是我想将该纹理放置在一个基于网格的单元格中(使用该特定单元格的屏幕坐标)我已创建。

任何人都可以指导我应该如何去做?

尝试将每个网格单元格变成一个对象。

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();