9
\$\begingroup\$

I am making a tiled based game, I want not to let the camera show places, where there is no map... this is a obvious solution I got from a tutorial

if(camera.position.x<400){
    camera.position.x=400;
}else if(camera.position.x>map.width*map.tileWidth-400){
    camera.position.x=map.width*map.tileWidth-400;
}

if(camera.position.y<240){
    camera.position.y=240;
}else if(camera.position.y>map.height*map.tileHeight-240){
    camera.position.y=map.height*map.tileHeight-240;
}

camera.update();

but I don't know how to get width & height of the tiledMap in the latest version of libgdx

https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/maps/tiled/TiledMap.java

Seems strange... I even tried the Super classes but couldn't find. Please help.

\$\endgroup\$

2 Answers 2

20
\$\begingroup\$
TiledMap tiledMap = new TmxMapLoader().load("path/to/tiled/map.tmx");   
MapProperties prop = tiledMap.getProperties();

int mapWidth = prop.get("width", Integer.class);
int mapHeight = prop.get("height", Integer.class);
int tilePixelWidth = prop.get("tilewidth", Integer.class);
int tilePixelHeight = prop.get("tileheight", Integer.class);

int mapPixelWidth = mapWidth * tilePixelWidth;
int mapPixelHeight = mapHeight * tilePixelHeight;

The mapWidth and mapHeight are the dimensions of the map in tiles. So they would both be 10 in a 10x10 grid. The tilePixelWidth and tilePixelHeight are the dimensions of the tiles in pixels. In a 32x32 tileset they would both be 32. Multiplying the dimensions together you will get the map dimensions in pixels.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Hi there, mobo! Could you explain a little bit more about the snippet? It might help others. \$\endgroup\$ Commented Jun 13, 2013 at 14:05
  • 2
    \$\begingroup\$ MapProperties is just an Object map that holds any properties of the TileMap. If you are using a .tmx file its any of the properties in the first line: width, height, tilewidth, tileheight, orientation are all there by default. The cod was wrong and I am fixed it. \$\endgroup\$
    – mobo
    Commented Jun 13, 2013 at 14:19
  • \$\begingroup\$ Thanks a lot - your contribution is appreciated I'm sure. :) \$\endgroup\$ Commented Jun 13, 2013 at 14:38
0
\$\begingroup\$

The information you seek is in the object of TiledMapLayer.

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

or

TiledMapTileLayer layer = (TiledMapTileLayer)tiledMap.getLayers().get("my_layer");

then you can use:

  • layer.getTileWidth(); to access tile width in pixels.
  • layer.getTileHeight(); to access tile height in pixels.
  • layer.getWidth(); to access number of tiles, horizontally.
  • layer.getHeight(); to access number of tiles, vertically.
  • layer.getTileWidth() * layer.getWidth(); to work out the map width in pixels.
  • layer.getTileHeight() * layer.getHeight(); to work out the map height in pixels.

To set change a specific tile,

TiledMapTile tile = null;
int column = 0;
int row = 0;
Cell cell = new Cell();
cell.setTile(tile);
layer.setCell(column, row, cell);

and to get a specific tile,

int column = 0;
int row = 0;
Cell cell = layer.getCell(column, row);
TiledMapTile tile = cell.getTile();
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .