Phob1a
Phob1a

Reputation: 773

How to get the position of a tiledmap cell in libgdx

I have an isometric tiled map and I'd like to get the position (x, y) of a given cell in the screen. Is this possible?

cell = ((TiledMapTileLayer) map.getLayers().get(0)).getCell(0, 0);

How do I get the position of this cell so I can place a sprite on top of it?

Upvotes: 1

Views: 4052

Answers (2)

Phob1a
Phob1a

Reputation: 773

I ended up making a parser that gets the tiled file exported as JSON and imports every tile as an extended scene2D actor which has all the methods and variables I need. I can even parse tile information such as the kind of terrain the tileset is or the altitude of the tile (I'm working with an isometric map that has multiple height levels)

I believe this to be the best solution in my case. I'm not posting any code because it's very case specific so if anyone wants to use this aproach you'll have to study the json file you generate in order to parse it correctly. Anyway, If someone needs it for reference just send me an email.

Upvotes: 0

Angel Angel
Angel Angel

Reputation: 21678

This a simple test.

public void chageTile(float xActor, float yActor){

     int tiledPx = 32; //yourPixelPerTile.

     int ajusteX = xActor / tiledPx;
     int ajusteY = yActor / tiledPx;


     //get(0) is layer 0 in your tilemap
     TiledMapTileLayer tileid = (TiledMapTileLayer)mapActual.getLayers().get(0);

     TiledMapTileLayer.Cell cell = new TiledMapTileLayer.Cell();

     //mapActual is your reference a your mapTile
     //MoldeTileSet is a name of the tileset in your map
     // and getTile(3) is a numbre 3 of de tileset image

     cell.setTile(mapActual.getTileSets().getTileSet("MoldeTileSet").getTile(3));

     Gdx.app.log("ChangeTileInPosPlayer", "");                  
     tileid.setCell(ajusteX, ajusteY, cell);    

     Gdx.app.log("ChangeTileInPosPlayer", "ajusteY +1");
     tileid.setCell(ajusteX, ajusteY + 1, cell);      

}

you might want to filter the tiles you want to change using propiedes, ect, so this is not null or anything

 if (tileid.getCell(ajusteX, ajusteY).getTile().
     getProperties().get("YourNameInTilePropertires", String.class) != null ){

        tileid.setCell(ajusteX, ajusteY, cell);      
}

if you have any errors or not working warns that, eh I could not test right now, I hope to help.

P.S: this is more or less what, I use for ortogonal tile, never used test for a tiled isometric, I think it can work or be more or less equal.

Upvotes: 1

Related Questions