Skip to content
tomcashman edited this page Feb 24, 2013 · 1 revision

Mini2Dx comes with built-in support for rendering Tiled maps.

Loading a Tiled map

The following code shows how to load a Tiled map named 'example.tmx' and render it to the screen.

	private TiledMap tiledMap;

	@Override
	public void initialise() {
		try {
			tiledMap = new TiledMap(Gdx.files.internal("example.tmx"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void render(Graphics g) {
		g.setBackgroundColor(Color.WHITE);
		//Draw the map at 0,0
		tiledMap.draw(g, 0, 0);
	}

Rendering sprites between layers Developers can extend the TiledMap class to allow for rendering of characters between layers and during map rendering.

public class GameMap extends TiledMap {
	public GameMap(FileHandle fileHandle) throws IOException {
		super(fileHandle);
	}

	@Override
	protected void onLayerRendered(TileLayer layer, int startTileX,
			int startTileY, int width, int height) {
		if(layer.getName().compareTo("COLLISIONS") == 0) {
			//TODO: Render sprites
		}
	}
}

Clone this wiki locally