Tiled Map Editor (Part 2) March 17, 2010
Posted by Jesse in : Game Development , 3 commentsIf you haven’t already, read part one first.
Now that we can load Tiled maps into our game, what can we do with the data? In his article, Nick mentioned that you can create layers dedicated to rectangles with attached metadata. In the editor, these are called object layers. (In the code, the type is MapObjectLayer, containing a collection of type MapObject.)
Tiled Map Editor (Part 1)
Posted by Jesse in : Game Development , 2 commentsA couple of weeks ago Nick Gravelyn posted a really great article explaining why he had given up on writing his custom level editor in favor of using the Tiled Map Editor. Along with the article, he uploaded some source code for reading Tiled maps into an XNA Game Studio based game. Since I’ve been procrastinating lately on my next game, I downloaded Tiled, grabbed Nick’s code and started tinkering.
Using some of my existing code, I’ve got a basic platformer up and running in just a few hours of work. If you’re interested in writing a tile based game, I’d recommend checking it out. However, I have a couple of note on using Tiled and Nick’s code:
- Layer names must be unique.
- Don’t use a tile size that doesn’t match.
- Be aware of file path issues.
Internally, the layers are stored in a Dictionary, with the layer name as the key. If you happen to have two layers with the same name (for example they might be named “New Layer” if you failed to rename them) you will get a thrown exception.
For some reason, when you create a tileset, Tiled allows you to specify a size different from the size you used when you created your map. The result in Tiled looks somewhat strange to me, and the example renderer in Nick’s code doesn’t handle it in quite the same way. I think this is an odd feature anyway, so my advice is to just don’t do it unless you really need to.
Nick included the ability to set a custom tileset path in the map processor, which is really handy. Unfortunately, Tiled stores the tileset path using relative paths, which can confuse Path.Combine. If you keep the map and tileset in the same folder, you can ignore this problem.
Alternatively, you can slightly modify the map processor. In the file TmxProcessor.cs, change line 27 from:
string path = string.IsNullOrEmpty(TileSetDirectory) ? tileSet.Image : Path.Combine(TileSetDirectory, tileSet.Image);
to:
string path = string.IsNullOrEmpty(TileSetDirectory) ? tileSet.Image : Path.Combine(TileSetDirectory, Path.GetFileName(tileSet.Image));
That’s it for now. In part two, I’m going to take a look at how you might go about creating game objects from the object layers in your Tiled map.