javalibgdxspritetiledtmx

create a Sprite list from a tmx map (TiledMap) in LibGdx


After many search on the internet I cannot find the answer ..

What is the situation ? I'm a student in computer science at university (in belgium) and i have to make a tower defense in Java with the graphic library LibGdx.

What is the problem ? In LibGdx, there is a map object named "TiledMap". It allows to load a tmx map (made with tiled for example..) So i load this map with this code :

TiledMap tiledMap = new TmxMapLoader().load(name);

And i render it with this code :

TiledMapRenderer tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap);
OrthographicCamera camera = new OrthographicCamera(960, 900); // Size of map
camera.position.set(posX, posY, 0);
camera.update();
tiledMapRenderer.setView(camera);
tiledMapRenderer.render();

But i don't found a method or something that allows me to separate the tmx map on a list of Sprite.. like

"private Sprite[][] tiles" .. this attribute will allow to change a tile at the position (0,0) by the sprite of a tower (if the player wants to build a tower to this position)

and also with properties used to make the tmx map i'll able to do another attribute like

"private boolean[][] isWay" .. to know if the tile (0,0) is the way (monsters can walk only if the tile is way)..

So i don't know how to change the TiledMap to Sprite[][]

I'm sorry if i do some english mistakes but i try to improve my english everyday...

Thank you for your help ;)


Solution

  • TiledMap doesn't have any Sprite instances. You can get refererence to a TiledMapTileLayer via TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().get(0) which would get the first layer.

    With that, you can get and set the cells (and the tiles they contain) via layer.getCell(x, y) and layer.setCell(x, y, cell).