Accessing A Specific Cell’s Tile In LibGDX

I have started to create a 2D game in LibGDX using a map(32×32 8 pixel tiles) from tiled map editor. In the method generateMushrooms, I want to access the cell(1,2) in layer 1 and set its tile to another tile in my tileset sprites.png with the id of 8. How would I achieve this? Right now I am getting a null pointer exception.

public void create() {

float w =;
float h =;
camera = new OrthographicCamera();
camera.setToOrtho(false, 256, 256);
tiledMap = new TmxMapLoader().load("custom.tmx");
tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap);
sb = new SpriteBatch();
texture = new Texture(Gdx.files.internal("badlogic.jpg"));
sprite = new Sprite(texture, 50, 50);

public void render() {, 0, 0, 0);, GL20.GL_ONE_MINUS_SRC_ALPHA);;


public void generateMushrooms() {

//Get first layer

TiledMapTileLayer layer = (TiledMapTileLayer) tiledMap.getLayers().get(1);

// Get cell at row 1 column 2

TiledMapTileLayer.Cell cell = layer.getCell(1, 2);

// Get Tileset

TiledMapTileSet tileSet = tiledMap.getTileSets().getTileSet("sprites.png");

//Set tile of id 8 for the cell


//Set the cell back in the layer at their x and y positions

layer.setCell(8, 16, cell);



Solved my problem! I have a traversing for loop go through the tiles and replace certain tiles like this (note that cur is a layer in the map).

for(int i=0; i < row; i++)
    for(int j=0; j < column; j++)
        cell = cur.getCell(i, j);
        cur.setCell(i * 8, j * 8, cell);
        cell.setTile(new StaticTiledMapTile("Some type of TextureRegion/Texture");

Leave a Reply

Your email address will not be published. Required fields are marked *