2D Array (Minor Question)

Monkey Forums/Monkey Beginners/2D Array (Minor Question)

Matt Finch(Posted 2014) [#1]
Hi everyone,

Please see the code below (I've only included what I believe is important for this question).


	Const TILE_W:Int = 40
	Const TILE_H:Int = 40
	
	Const MAP_W:Int = 1920
	Const MAP_H:Int = 1920
	
	Field mapArray:MapObj[ ( MAP_W / TILE_W ) * ( MAP_H / TILE_H ) ] 
	
	Method OnCreate:Int()
		SetUpdateRate( UPDATE_RATE )
				'initialise all map array elements with x and y positions and tile 1
				For Local x:Int = 0 To ( MAP_W / TILE_W )
					For Local y:Int = 0 To ( MAP_H / TILE_H )
						mapArray[ x + ( y * ( MAP_W / TILE_W ) ) ] = New MapObj( x * TILE_W, y * TILE_H, 1 )
					Next
				Next
		Return( 0 )
	End


The mapArray is populated almost as I expect, except the first column is not (apart from element 0,0), after some head-scratching I can't seem to fathom why this is missing. Might be easier as a diagram:

X X X X X X X
0 X X X X X X
0 X X X X X X
0 X X X X X X

X = displayed as expected
0 = object not created as part of the array.


Matt Finch(Posted 2014) [#2]
***RESOLVED***

Needed to add a '- 1' to both of the For...Next... loops

:)


Beaker(Posted 2014) [#3]
Or use Until instead of To.