Java - Libgdx -TiledMap | Java - Libgdx -TiledMap |获取 map 中每个单元格的 screenX 和 screenY

标签 java libgdx game-development tiled tmxtiledmap

正如标题所示,我正在尝试获取 map 中每个单元格的屏幕坐标。有可能吗? 我真的很沮丧,想不通! 我感谢您的帮助! 请注意,我的 map 在屏幕上有一个静态位置。

另一个注意事项:我有一个扩展 Actor 的自定义类 Cell。我这样做是为了让我的单元格可点击,它看起来像这样:

public class Cell extends Actor {
private TiledMapTileLayer.Cell tiledMapCell;
private Texture cellTexture;

public Cell(TiledMapTileLayer.Cell tiledMapCell, Field field){
    this.tiledMapCell = tiledMapCell;
    this.field = field;
    this.cellTexture = tiledMapCell.getTile().getTextureRegion().getTexture();
}

public TiledMapTileLayer.Cell getTiledMapCell(){
    return this.tiledMapCell;
}

public Texture getCellTexture(){
    return this.cellTexture;
}

public void setCellTexture(Texture texture){
    this.cellTexture = texture;
    this.tiledMapCell.setTile(new StaticTiledMapTile(new TextureRegion(cellTexture)));

谢谢!!

最佳答案

你可以这样处理:

在您的 Cell 类中添加一个新变量来获取图 block 的位置。

    private Vector2 pos;

更改您的构造函数。

    public Cell(TiledMapTileLayer.Cell tiledMapCell, Field field, Vector2 pos) {
        this.tiledMapCell = tiledMapCell;
        this.field = field;
        this.cellTexture = tiledMapCell.getTile().getTextureRegion().getTexture();
        this.pos = pos;
    }

在您的主类中获取 map 宽度中的图 block 数量,对图 block 的高度和大小执行相同的操作。

    int mapWidth = tiledMapTileLayer.getWidth();
    int mapHeight = tiledMapTileLayer.getHeight();
    int tileSize = tiledMapTileLayer.getTileWidth();

现在使用循环

    // These variables will store the coordinates of each new tile. and will be changed at each loop turn.
    int tempX = 0;
    int tempY = 0;

    for (int i = 0; i < mapWidth * mapHeight; i++) {
        pos = new Vector2 (tempX + tileSize / 2, tempY + tileSize / 2);

        // Create a new cell with your Cell class, pass it the position.    
        new Cell(TiledMapTileLayer.Cell tiledMapCell, Field field, pos)

        // Increase the tempX
        tempX += tileSize;
        // If the first line is finish we go to the next one.
        if (tempX > mapWidth * tileSize) {
            tempY += tileSize;
            tempX = 0;
        }
    }   

这是我使用的方法,因为我没有找到任何其他方法。

关于Java - Libgdx -TiledMap | Java - Libgdx -TiledMap |获取 map 中每个单元格的 screenX 和 screenY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56434167/

相关文章:

java - 关于我的数组代码

java - 使用 Java 对 URL 参数进行编码

c# - 如何使用 C# 在 Unity 3D 中随机时间(相同位置)生成敌人?

java - 如何在棋盘游戏中序列化(反序列化)改变游戏规则的纸牌行为?

python - 在 Python 中创建类对象属性后如何更改它们?

java - Hudson 已经停止检查来自 assembla 的源代码

java - 为什么 Double.parseDouble(scan.next()) 比 scan.nextDouble() 快这么多?

java - Libgdx - 纹理未显示在屏幕更改上

java - 每次在 libgdx 中单击按钮时,如何向前和向后移动 Sprite?

java - 从 LibGDX Applet 访问 cookie