繁体   English   中英

在没有tmx的情况下使用Libgdx为地图创建纹理的正确方法是什么?

[英]What is the correct way of creating textures for a map with Libgdx without tmx?

我正在使用LibGdx制作自上而下的游戏,我希望每个新游戏加载都会随机生成。

我正在使用一个内部有8 x 8子画面的子画面表,需要将它们组合成16 x 16的图块。 (我这样做是为了获得更自然的外观水平。如有需要,我可以进一步解释。)

我已经有了算法来为哪个图块应该是什么生成数组。

但是我坚持应该如何处理磁贴类。

我有一个叫做Tile的课程

import com.badlogic.gdx.graphics.g2d.Sprite;


public class Tile extends Sprite{ // Should I use sprite??? Seems wrong..

public byte id;

public static Tile[] tiles = new Tile[256];

public static Tile grass = new GrassTile(0);

public Tile(int id) {
    this.id = (byte) id;
    if (tiles[id] != null)
        throw new RuntimeException("Tile Already Exists....");
    tiles[id] = this;
}

}

我想有多个类可以为每个磁贴扩展该类。 例如,这是草。

import com.badlogic.gdx.graphics.g2d.Sprite;


public class Tile extends Sprite{

public byte id;

public static Tile[] tiles = new Tile[256];

public static Tile grass = new GrassTile(0);

public Tile(int id) {
    this.id = (byte) id;
    if (tiles[id] != null)
        throw new RuntimeException("Tile Already Exists....");
    tiles[id] = this;
}

}

这些类是否应该从Sprite扩展? 还是来自libgdx的其他类?

同样要记住,这些瓦片将是16 x 16,由sprite板的4个较小的8x8区域组成。

Libgdx github页面提供的一个很好的示例向您展示了如何通过生成TileMapLayer来实现这一点。 将单元分配给图层,然后将图层添加到地图。

此代码段将帮助您实现所需的功能。 原始文档在这里

此处提供更多文档: Libgdx Tile-maps

  public void create () {
            float w = Gdx.graphics.getWidth();
            float h = Gdx.graphics.getHeight();

            camera = new OrthographicCamera();
            camera.setToOrtho(false, (w / h) * 320, 320);
            camera.update();

            cameraController = new OrthoCamController(camera);
            Gdx.input.setInputProcessor(cameraController);

            font = new BitmapFont();
            batch = new SpriteBatch();

            {
                tiles = new Texture(Gdx.files.internal("data/maps/tiled/tiles.png"));
                TextureRegion[][] splitTiles = TextureRegion.split(tiles, 32, 32);
                map = new TiledMap();
                MapLayers layers = map.getLayers();
                for (int l = 0; l < 20; l++) {
                    TiledMapTileLayer layer = new TiledMapTileLayer(150, 100, 32, 32);
                    for (int x = 0; x < 150; x++) {
                        for (int y = 0; y < 100; y++) {
                            int ty = (int)(Math.random() * splitTiles.length);
                            int tx = (int)(Math.random() * splitTiles[ty].length);
                            Cell cell = new Cell();
                            cell.setTile(new StaticTiledMapTile(splitTiles[ty][tx]));
                            layer.setCell(x, y, cell);
                        }
                    }
                    layers.add(layer);
                }
            }
            // for top down camera view
            renderer = new OrthogonalTiledMapRenderer(map);

        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM