Tiled Maps

libgdx contains built-in support for tiled maps.  If you look inside com.cmein.tilemap.utils, you will see several classes, none of which we wrote (thanks libgdx!).  If you open up TiledMapPacker.java, you’ll notice it has a runnable main method.  In this main method, there are only three lines that need to be edited to create a tiled map:

tmxFile = new File("data/tiledmaps/LEVEL2.tmx");
baseDir = new File("data/");
outputDir = new File("data/");

The file extension “.tmx” may be foreign to you; it is associated with “Tiled”, a program to help the process of making tiled maps (see http://www.mapeditor.org/).  The main method in TiledMapPacker takes the .tmx file and outputs two new files.

First, it outputs a png image containing all the necessary tiles for your tiled map.  The image will look something like this:

Clearly this is not a tiled map; rather, it is a collection of tiles that  can be used to produce a tiled map.   The second file is called a packfile, and it stores the information regarding which tile goes where in the final tiled map.  Our tiled maps are 15×10 tiles, which seems to be a best fit for most Android devices.  Now that we have the necessary data for our tiled map, we need to connect the png file and the packfile, and produce something that we can render from our application.  Take a look at the Art class again, this time at the loadLevel method:

public static TiledMapRenderer loadLevel (FileHandle mapHandle, FileHandle packFile, FileHandle baseDir) {
int blockWidth = MAP_WIDTH/3;
int blockHeight = MAP_HEIGHT/3;

startTime = System.currentTimeMillis();
map = TiledLoader.createMap(mapHandle); //MAKE MAP
endTime = System.currentTimeMillis();
System.out.println("Loaded map in " + (endTime - startTime) + "mS");

atlas = new TileAtlas(map, packFile, baseDir);

startTime = System.currentTimeMillis();
renderer = new TiledMapRenderer(map, atlas, blockWidth, blockHeight);
endTime = System.currentTimeMillis();
System.out.println("Created cache in " + (endTime - startTime) + "mS");

return renderer;

This method takes as input the png file and packfile produced by TiledMapPacker, and it returns a renderer which can be used to render the map.

This process of producing a tiled map may seem long and tedious; however, the Tiled software is very intuitive and easy to use, and once you have learned the process of creating one map, subsequent maps become much easier to make.

Coming soon:

1) More specifics about how we created towers, bullets, and enemies,  and how they interact with each other (we have a ton of working code, but we need to smooth out some rough edges).

2) Enemy pathing.  We have a system in which we can give them a series of waypoints to follow, but we would like a more sophisticated system allowing the player to build mazes with their towers.  The enemies will have to be able to “figure out” a way through the maze.

3) Level/wave system:  Right now, we have one level, and we are trying to perfect it before continuing.  In the end, we would like several levels, each of which contains several waves of enemies.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: