-
Notifications
You must be signed in to change notification settings - Fork 42
Tiled Maps
Mini2Dx comes with built-in support for rendering Tiled maps.
First add the mini2Dx-tiled dependency to your core project.
project(":core") {
...
dependencies {
...
compile "org.mini2Dx:mini2Dx-tiled:$mini2DxVersion"
...
}
}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);
}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
}
}
}When you're finishing with your TiledMap instance, it is good practice to dispose of the resource and free up memory.
tiledMap.dispose();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>Getting Started
Graphics
Gameplay
Data
- Asset Management
- Saving and loading player data
- Converting objects to/from JSON
- Converting objects to/from XML
User Interfaces / HUDs
Structuring your code
Releasing your game