Game Development Reference
In-Depth Information
Simple Tile Maps
Certainframeworks,suchascocos2d,havebuilt-insupportfortilemapsgenerated
bythefreeprogramTiled( www.mapeditor.org ).It'sagreatprogram,andIheartily
recommend it if your framework of choice supports it. However, if Tiled is not
supported by a particular framework, it still is possible to implement tile maps
without major difficulty. This is especially true if each level fits on a single screen
and scrolling isn't required.
The first order of business is to determine the size of the tiles. This will vary de-
pending on the device, but a popular size for many smartphone games is 32×32
pixels. This means that on a Retina iPhone with a resolution of 960×640, the
screen would be able to fit 30×20 tiles.
Once the size of the tiles has been settled, the next step is to create the tile set that
the game will use. All of the tiles should be placed into a single sprite sheet, if
possible, for the gains outlined earlier. The code to render the tile map then needs
some way to convert from the unique tile ID into the actual tile image. A simple
convention is to have the tile in the top-left corner of the sprite sheet be tile 0, and
then continue numbering across the row. The numbering for subsequent rows then
continues at the number where the previous row left off.
Once we have a tile set, we can then create level data using these tiles. Although
the level data could be a hardcoded 2D array of integers, it will work better if the
level data is stored in an external file. The most basic file format would be a text
file that lists the tile ID of each tile at each location on the screen. So a level that's
5×5 tiles in size may look like this:
// Basic level file format
5,5
0,0,1,0,0
0,1,1,1,0
1,1,2,1,1
0,1,1,1,0
0,0,1,0,0
In this file format, the first line is ignored so the user can write a comment. The
second line specifies the length and width of the level, and then the raw tile ID
data is listed for each row and column. So the first row for this level has two tiles
with ID 0, one with ID 1, and then two more with ID 1.
Levels would then be represented by the following class:
Search WWH ::




Custom Search