我正在尝试在 libgdx/android studio 中的平铺 map 上渲染 Sprite /纹理。请注意,我的 map 是 20x25,具有 8 个像素的图 block ,splitTiles 是一个纹理区域。
public void show() {
cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
cam.setToOrtho(false, 20, 25);
batch = new SpriteBatch();
map = new TmxMapLoader().load("centipedeMap.tmx");
renderer = new OrthogonalTiledMapRenderer(map,1/8f)
}
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.setView(cam);
renderer.render();
player.update();
ex.update();
batch.setProjectionMatrix(cam.combined);
cam.update();
batch.begin();
Texture tilesImage = new Texture(Gdx.files.internal("tile.png"));
TextureRegion[][] splitTiles = TextureRegion.split(tilesImage, 8, 8);
batch.draw(splitTiles[0][0],50,50);
batch.end();
}
最佳答案
您的相机视口(viewport)尺寸为 20, 25,并且您在 50,50 处绘制 TextureRegion,因此您的图像呈现在屏幕之外,因此不可见。
推荐:Texture是重物,因此尽量避免创建相同纹理的多个实例。
您的相机视口(viewport)尺寸为 20,25,与 tileMap(20,25) 尺寸相同,因此相机 1 单位表示 TileMap 的一个单元格。
您可以检测与 Tiles 的碰撞,无需创建 Rectangle 对象。
关于java - 在 tilemap 上使用 Libgdx 渲染 Sprite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43577382/