Game Development Reference
In-Depth Information
m_TileList.Add(new Tile() { IsSolid = false,
SheetPosX = 4, SheetPosY = 0 });
m_TileList.Add(new Tile() { IsSolid = false,
SheetPosX = 5, SheetPosY = 0 });
m_TileList.Add(new Tile() { IsSolid = false,
SheetPosX = 6, SheetPosY = 0 });
m_TileList.Add(new Tile() { IsSolid = true,
SheetPosX = 7, SheetPosY = 0 });
The first line calls our LoadBitmap() method again to load in the tile sheet and
store it in the m_TileSheet member variable. The second line creates our tile list
object. This will store information for each tile type. The eight lines of code at the bot-
tom create entries in the tile list for all of the tiles in the first row of the tile sheet. Of
course, the tile sheet has more than one row of tiles in it, but I will not show the code
for the other rows here, since it is very similar and would take up several pages.
We have one more thing to do to finish initializing the game world. It consists of ini-
tializing the map. The map is simply a two-dimensional array. Each element in the
array represents a tile position in the game world. As such, the array is of type int ;
it is of type int because each element stores a numeric index in the tile list. So ba-
sically, each element in the array holds a number that tells us which type of tile is at
this position in the game world. As the code that fills in this array is much too wide to
fit on the page, I will show a brief example of how it is initialized here:
m_Map = new int[,] { {14, 14, 14 },
{14, 0, 14 },
{14, 14, 14 } };
As you can see, we are creating a new two-dimensional int array. In this sample
code, we have a 3 x 3 world. We are using tile type 14 (a brick wall tile) to make a
wall around the outer border of this small world. In the center, we have tile type 0 ,
which in our game demo is a grass tile. Each row of values gets its own pair of en-
closing brackets ( {} ), followed by a comma. This is basically how you set up a 2D
tile map. Of course you can get a lot fancier with this. For example, you can imple-
ment animated tile types in your game. These would be animated very similarly to
how we will animate our robot character. Check out the downloadable code for this
Search WWH ::




Custom Search