Skip to content
Thomas Cashman edited this page Jul 11, 2019 · 9 revisions

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

Set Up

First add the mini2Dx-tiled dependency to your core project.

project(":core") {
        ...
        dependencies {
                ...
                compile "org.mini2Dx:mini2Dx-tiled:$mini2DxVersion"
                ...
        }
}

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
		}
	}
}

Disposing of a TiledMap

When you're finishing with your TiledMap instance, it is good practice to dispose of the resource and free up memory.

tiledMap.dispose();

Tiles tearing when scaled

A common problem when scaling a TiledMap is that there are visual gaps between tiles. This is caused by float rounding when scaling images. The solution to this is to pad your tiles with an extra pixel around its borders. The tilepacker tool can automatically do this for you. The following is an example tilepacker config that automatically pads your tile images.

<tilePackerConfig>
   <tileWidth>8</tileWidth>
   <tileHeight>8</tileHeight>
   <tilesetWidth>256</tilesetWidth>
   <tilesetHeight>256</tilesetHeight>
   <tilePadding>2</tilePadding>
   <outputFormat>PNG</outputFormat>
   <outputPath>./core/assets</outputPath>
   <preventTearing>true</preventTearing>
   <tiles class="java.util.ArrayList">
   	  <string>./tile1.png</string>
   	  <string>./tile2.png</string>
   </tiles>
</tilePackerConfig>

Clone this wiki locally