day 10 – tilemaps

published on: 10. December 2012

To create tilemaps we first need to install the Tiled Map Editor:
(when you have linux you may have to compile it yourself. everything you need to know is in the README file)

After we start the program, we create a new map via File > New. Here we have to specify the size of the map and the size of the tiles.

Now we need tiles, that our map consists of. I downloaded this tileset and put it into a new subfolder maps of my stuff folder.
In Tiled we now click Map > New Tileset and select our tilesetfile to be the tileset.

In the right collumn we can see the layers and the tiles.
We create a layer for objects and one layer for the ground and then create a nice little map. To draw a tile, just select the layer and the tile and then click into the left part of the screen on the fitting place.

Then we save our map as a .tmx file into the stuff/maps folder where our tileset-Image is.

As for textures there is a map-packer in libgdx:

java -classpath [path tol ibgdx]/gdx.jar:[path to libgdx]/extensions/gdx-tools.jar:[path to libgdx]/extensions/gdx-tiled-preprocessor.jar com.badlogic.gdx.tiledmappacker.TiledMapPacker [path to stuff]/maps [path to testgame-android]/assets/maps 

If the map is packed in our assets-folder, it’s really easy to draw it:

TiledMap map;
TileAtlas atlas;
TileMapRenderer tileMapRenderer;
OrthographicCamera camera;
public IngameScreen(){
	// load tilemap
	map = TiledLoader.createMap(Gdx.files.internal("maps/testmap.tmx"));
	atlas = new TileAtlas(map, Gdx.files.internal("maps"));
	tileMapRenderer = new TileMapRenderer(map, atlas, 8, 8); // handles rendering the tile map. 8x8 is the chunk size to preload
	camera=new OrthographicCamera(800,480);

public void render(float delta) {,1,1, 1);

	// move map
	// render map

Seems pretty easy to me. If you though got problems, write a comment.

posted in libgdx

4 Responses to “day 10 – tilemaps”

  1. M says:

    When I try to run the texture packer I got the following. Any clues?
    Exception in thread “main” java.lang.NoClassDefFoundError: 4
    Caused by: java.lang.ClassNotFoundException: 4
    at$ Source)
    at Method)
    at Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    Could not find the main class: 4. Program will exit.

    • dem n00bz says:

      Der TiledMapPacker will nicht laufen. Egal was ich mache, ich erhalte nur folgende Meldung:

      Exception in thread “main” java.lang.NoClassDefFoundError: com/badlogic/gdx/tiledmappacker/TiledMapPacker
      Caused by: java.lang.ClassNotFoundException: com.badlogic.gdx.tiledmappacker.TiledMapPacker
      at Method)
      at java.lang.ClassLoader.loadClass(
      at sun.misc.Launcher$AppClassLoader.loadClass(
      at java.lang.ClassLoader.loadClass(
      Could not find the main class: com.badlogic.gdx.tiledmappacker.TiledMapPacker. Program will exit.

      • dem n00bz says:

        I know what’s wrong:
        It’s not extensions/gdx-tools.jar, the jar is in another folder with the same name!
        The full command looks like this for me:

        java -classpath /home/marcel/Downloads/libgdx-0.9.8/gdx.jar:/home/marcel/Downloads/libgdx-0.9.8/extensions/gdx-tools/gdx-tools.jar:/home/marcel/Downloads/libgdx-0.9.8/extensions/gdx-tiled-preprocessor/gdx-tiled-preprocessor.jar com.badlogic.gdx.tiledmappacker.TiledMapPacker $1 $2

        • dem n00bz says:

          The only problem i encounter now is that it doesnt render anything. I dont get any error messages, everything seems to work. But the screen stays black :( src is the same

Leave a Reply