0
\$\begingroup\$

I added some tiles to some object layers in my tiled map and now I want to draw them in game. I tried to render everything with

mapRender.render();

but the tiles doesn't show up. Then I tried to render just one object layer and all of its objects with

mapRenderer.renderObjects(tiledMap.getLayers().get(0));

but it wasn't working.

Ingame: Ingame

Tiled: Tiled

Could someone please tell me how to make the tiles show up? Thank you very much!

\$\endgroup\$
1
\$\begingroup\$

All current implementations of TiledMapRenderer, such as BatchTiledMapRenderer, do not have the functionality to render any MapObject. You'll have to create your own implementation of TiledMapRenderer. This should get you started:

public class TextureMapObjectRenderer extends OrthogonalTiledMapRenderer {

    public TextureMapObjectRenderer(TiledMap map) {
        super(map);
    }

    public TextureMapObjectRenderer(TiledMap map, Batch batch) {
        super(map, batch);
    }

    public TextureMapObjectRenderer(TiledMap map, float unitScale) {
        super(map, unitScale);
    }

    public TextureMapObjectRenderer(TiledMap map, float unitScale, Batch batch) {
        super(map, unitScale, batch);
    }

    @Override
    public void renderObject(MapObject object) {
        if (object instanceof TextureMapObject) {
            TextureMapObject textureObject = (TextureMapObject) object;
            batch.draw(
                    textureObject.getTextureRegion(),
                    textureObject.getX(),
                    textureObject.getY()
            );
        }
    }
}

I would also recommend against using Tile Objects to render sprites in-game, as they're more commonly used for Tiled purposes and identifying entities that you want to spawn in-game. This video explains what I mean here.

This depends on the type of game, however. This is an example of using an Object Layer with Tile Objects for creating levels in a Breakout game; it makes it very easy for Box2D bodies to be created and the bricks to be drawn:

Tile Objects in an Object Layer

\$\endgroup\$
  • \$\begingroup\$ Thank you! ... this is very helpful, but I got a new little problem... I get everything drawn now, but I scaled my sprites and I need to get the scale in code for the right resize of the sprite... I use normal rectangle map objects to spawn entities ;) \$\endgroup\$ – Richard H Jul 9 '15 at 21:19
  • \$\begingroup\$ @RichardH Check out TextureMapObject, it contains methods getScaleX() and getScaleY() which may be of use to you, but I am not entirely sure of their reliability. \$\endgroup\$ – driima Jul 9 '15 at 21:28
  • 1
    \$\begingroup\$ I just want to say that while indeed tile objects can be used as spawn indicators, using them for actual visual decoration of the map is totally fine and also an expected use-case. It is why in since Tiled 0.10 you can rotate these objects and since Tiled 0.12 they are also resizable. \$\endgroup\$ – Thorbjørn Lindeijer Jul 9 '15 at 21:30
  • \$\begingroup\$ @ThorbjørnLindeijer ahh, I haven't updated in a while, so I didn't know about these new features! Thanks for the information. \$\endgroup\$ – driima Jul 9 '15 at 21:42
  • 1
    \$\begingroup\$ @RichardH I'm unsure then. Perhaps you can check textureObject.getProperties() for width and height properties? As far as I am aware, libGDX does deserialize this data. \$\endgroup\$ – driima Jul 10 '15 at 13:33
0
\$\begingroup\$

Please consider that the previous answer is not complete.

If you render object with the 3 param batch.draw(...), the TextureMapObject would be rendered without the provided in the TileMap scale and rotation. I suggest using a more accurate

batch.draw (TextureRegion region, float x, float y, float originX, float originY, float width, float height,
        float scaleX, float scaleY, float rotation);

in my kotlin implementation it looks like this

 override fun renderObject(obj: MapObject) {
        if (obj is TextureMapObject) {
            batch.draw(
                      obj.textureRegion,
                      obj.x,
                      obj.y,
                      obj.originX,
                      obj.originY,
                      obj.textureRegion.regionWidth.toFloat(),
                      obj.textureRegion.regionHeight.toFloat(),
                      obj.scaleX,
                      obj.scaleY,
                      obj.rotation)
        }
    }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.