Time for action — creating 2D arrays

Creating arrays with more than one dimension in Monkey is a little tricky. We can create a function that lets us set up a 2D array, using the following steps:

  1. Add the function Create2DArray to the treasurechest.monkey file. Its parameters are the number of cells required for both the dimensions. The return type is a 2D array of the type INT.
    Function Create2DArray:Int[][]( columns:Int, rows:Int)
    
  2. Define a local two-dimensional array of the type INT, initialized in the first dimension.
    Local a:Int[][] = New Int[columns][]
    
  3. Start a FOR loop ranging from 0 to columns.
    For Local c:Int = 0 Until columns
    
  4. Define a new array of rows in each column.
    a[c] = New Int[rows]
    
  5. Close the FOR loop, return the array, and close the function.
    Next
    Return a
    End
    

What just happened?

You have just created a function that will let you set up a 2D array in Monkey. It is reusable for other games that you will create in the future.

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

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