Creating 2D arrays

In this game, we want to store the tile map information inside a two-dimensional array. If you don't know what a 2D array is, imagine a table. The first dimension is represented by the rows and the second dimension by the columns. A 1D array is comparable with a table with just one column and several rows.

Here are two illustrations that show the difference:

Creating 2D arrays

In the preceding image, you see an array with one dimension. It has just one column and the data is stored in a sequence, row after row. The following image shows an array with two dimensions. The difference here is that the second dimension is achieved by another column. You have two slots per row to store data. Actually, you will have as many slots per row as you define.

Creating 2D arrays

To access an array, here are small examples:

Print (my1Darray[2]) 'With the array above, this will print 4
Print (my2Darray[0][1]) 'With the array above, this will print 5

Note

Hint: Arrays in Monkey are zero-based. This means that the first index of a dimension is 0. A dimension defined with 10 elements has indices ranging from 0 to 9.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.90.182