java - 2D Tiled Game - Using new bad data -
in 2d tiled game, have problem, when update object 2d array in loop inside (looping in 2d array top left bottom right, row row(like code below)), if program looping @ index (5,6) , need data object under itself, it'll use new data have executed when loop @ (5,5) want use data before start of double loop...
a basical example:
int[][] map = new int[10][10]; for(int x = 0; x < 10; x++) { for(int y = 0; y < 10; y++) { update(x, y, map); } } // remember example void update(int x, int y, int[][] m) { m[x][y] = 0 if(y > 9) { return; } m[x][y + 1] = 1 }
it put instantly data "1" @ (x, 10), without considering generate errors...(arrayoutofboundsexception...)
how can make use data of array when don't started double loop yet?
i know generata arrayoutofboundexecption, , single if can correct done here
int water = 1; int air = 0; int[][] map = new int[20][20]; void update() { for(int x = 0; x < 10; x++) { for(int y = 0; y < 10; y++) { tick(x, y, map); } } } void tick(int x, int y, int[][] m) { if(y > m.lenght - 1) { return; } m[x][y] = air; m[x][y + 1] = water; }
you saying iterating map row row when doing columns. try looping first y
, x
.
your update method wrong. y+1
when y = 9
try access map[x][10]
throw arrayoutofboundsexception
. remember array declared new int[10]
has 10 items starting 0 , ending @ position 9.
Comments
Post a Comment