Skip to main content
added 591 characters in body; edited body
Source Link
driima
  • 995
  • 4
  • 23

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

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()
            );
        }
    }
}

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

Source Link
driima
  • 995
  • 4
  • 23

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()
            );
        }
    }
}