【问题标题】:Rendering sprites in Libgdx on tilemap在 tilemap 上的 Libgdx 中渲染精灵
【发布时间】: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)。非常感谢,请发布答案,以便我接受。

标签: java android libgdx tile


【解决方案1】:

您的相机视口尺寸为 20、25,并且您在 50,50 处绘制 TextureRegion,因此您的图像在屏幕外渲染,因此不可见。

建议:Texture 是重物,所以尽量避免创建相同纹理的多个实例。

您的相机视口尺寸为 20,25,与 tileMap(20,25) 尺寸相同,因此相机 1 单位表示 TileMap 的一个单元格。

可以检测与 Tiles 的碰撞,不需要创建 Rectangle 对象。

【讨论】:

  • 应该是这样吗-if (layer.getCell(x, y) != null &&layer.getCell(x,y).getTile().getProperties().containsKey("Blocked")) {do something}
  • 玩家总是被八个单元格包围,检查你的玩家是否会碰撞那个单元格。检查该单元格具有什么属性或 ID,具体取决于您的操作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-27
  • 1970-01-01
  • 2016-03-23
  • 2017-11-17
  • 2015-10-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多