Game Development Reference
In-Depth Information
The tile sheet will be referenced as a single dimensional array of tiles. So, the tile in the upper
left-hand corner of Figure 6-1 will be tile 0, and the last tile in the grid will be file 31 (the lower
right-hand corner).
Organizing game levels into tiles
Our game levels will be made up of a 20
32-sized tiles. The grid will be encoded
into in a 2D array of tile ID numbers. The tile ID number will be the number 0-31 (see Figure 6-1)
that represents a tile id on the tile sheet. A 2D array is what we call an array of arrays.
ActionScript does not have built-in support for an array with more than a single dimension. We
will create a 2D array by placing another array inside each element of our base array. The level
screen size will be 640 (20
15 grid of 32
32)
480 (15
32). This means that our level data will consist of
300 (20
15) tile ID numbers. We will have 15 rows and 20 columns. Here is an example of the
2D data format for the level we will create:
var backGroundMap:Array = [
[26,24,25,0,26,26,26,26,26,0,0,26,26,26,26,26,0,24,25,26],
[27,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,27],
[29,0,28,0,0,0,24,25,0,24,25,0,24,25,0,0,0,27,0,29],
[27,0,28,0,27,0,0,0,0,0,0,0,0,0,0,27,0,29,0,27],
[29,0,0,0,29,0,0,30,0,0,0,0,31,0,0,29,0,0,0,29],
[0,0,28,0,26,0,0,31,0,0,0,0,31,0,0,26,0,26,0,0],
[0,0,0,0,0,0,0,31,0,0,0,0,31,0,26,0,0,0,0,0],
[26,26,26,0,0,26,0,30,30,30,30,30,30,0,0,0,0,26,26,26],
[0,0,0,26,0,0,0,0,0,0,0,28,0,0,0,0,26,0,0,0],
[0,0,0,0,26,0,0,27,0,27,0,27,0,28,0,26,0,0,0,0],
[27,0,0,0,27,0,0,29,0,29,0,29,0,28,0,27,0,0,0,27],
[29,0,0,0,29,0,0,0,0,0,0,0,0,0,0,29,0,0,0,29],
[27,0,0,0,0,26,0,28,0,28,0,28,28,0,26,0,0,0,0,27],
[29,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,29],
[26,24,25,0,0,26,26,26,26,0,0,26,26,26,26,0,0,24,25,26]
]
Notice that there are exterior brackets ( [] ) surrounding the interior rows of tile IDs (each also
inside their own set of brackets). The exterior set of brackets creates the array that will hold our
15 row arrays of column data. This creates a 2D array of rows and columns.
Our data will be organized into a set of rows and columns. We will use a 2D array for the level
data, because accessing the grid of tiles programmatically is made much easier if we can simply
reference the row and column the tile is located in. The row will be the first subscript of the 2D
array, and the column will be the second subscript. For example, the method to access the tile in
row 10 and column 5 would look something like this:
levelData[9][4] = 26
Since our arrays are zero-relative , meaning they start with 0 and not 1, 9 is the tenth row, and 4
is the fifth column. If you look closely at this, you might notice something odd. Rows are actually
the y axis dimension of our grid, and columns are the x axis dimension, but we are accessing
them by row and then column ( [y][x] ) rather than [x][y] . This has been debated for years by
various game developers. In our experience, it is much easier and more straightforward to create
tile-based worlds using the [row][column] context when using a 2D array in AS3. All you need to
do in the level data to find the correct tile is count down (the first subscript of the 2D array) to find
row 9 (starting at 0) and then count over (the second subscript in the 2D array) to find column 5
(starting at 0). The tile ID at row 9, column is 26.
Search WWH ::




Custom Search