O'Reilly logo

Monkey Game Development by Michael Hartlef

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required