【发布时间】:2017-04-23 22:51:45
【问题描述】:
我正在尝试在 libgdx/android 工作室中的平铺地图上渲染精灵/纹理。请注意,我的地图是 20x25 的,带有 8 个像素图块,而 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();
}
【问题讨论】:
-
那么问题是什么?
-
抱歉含糊不清,我的精灵没有出现在平铺地图的顶部。瓦片地图正在渲染,但精灵没有。
-
你能画简单的 textureRegion 而不是 Splited TextureRegion 吗?并且不要在
render方法中创建Texture对象。 -
感谢@AbhishekAryan,我在
show方法中初始化了Texture/TextureRegion,它成功了!我不明白的是批次的x和y坐标对应于单元格。例如'batch.draw(image,2,2,1,1);'将图像放在单元格 2,2 所在的位置。目前,我在地图上放置随机图块,并为碰撞设置矩形,例如,如果Cell=(1,1)放置矩形,则将矩形放置在 (1*8,1*8) 处。我应该将矩形放置在 (1,1) 还是应该放置 (1*8,1*8)。非常感谢,请发布答案,以便我接受。