libgdx How to replace a tile in a tilemap?

I want to replace a tile on my tilemap either into another one from my tileset or just remove it from the rendered tilemap once my player walks over that specific tile.

How would I do that?

What do you use to store your tilemap in? List, array of tiles or something else?

Its a Tiled tilemap.
I load it with the TmxMapLoader so I’m not creating an array “manually”

Take a look at this:

http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/maps/tiled/TiledMapTileSet.html

I think you can use the TiledMapTileSet.putTile(paramA, paramB); for what you need.
Note that I have no experience with LibGDX, so there might be better ways to do it.

  • hwin

Yeah I’ve been looking into the libgdx documentation for a while but cant figure out how to get it working.
Was hoping someone had a clear code example for me.

I found a way to do it:`

First i get the current cell my player is on from the player x,y pos. & then i check in the Tilelayer (named collisionlayer here) if that tile contains a property “point” (which I’ve added to the tile in Tiled).
Then if thats the case I change the Tile with the appropriate tile from my Tileset (#6 in my case).

	public void handleChangableTileCollsion(){
		Cell cell = collisionlayer.getCell(
				(int) ((x +10) / (collisionlayer.getTileWidth() )),
				(int) ((y +10)/ (collisionlayer.getTileHeight() )));
			
		if(cell.getTile().getProperties().containsKey("point")) {
			cell.setTile(map.getTileSets().getTile(6));
		}
	}

Additionally, if you want to remove a tile, you can just set it to null. On your TiledMapTileLayer use .setCell(x, y, null)