In my 2D Tiled game, I have a problem, when I update all the Object from a 2D array in a for loop inside another (looping in the 2D array from top left to bottom right, row by row(like the code below)), If the program is looping at index (5,6) and it need data from the Object under itself, It'll use the new data that he have executed when the loop is at (5,5) but I want to use the all data before the start of the double for 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);
}
}
// I remember you that it is an example
void update(int x, int y, int[][] m)
{
m[x][y] = 0
if(y > 9) { return; }
m[x][y + 1] = 1
}
It will put instantly the data "1" at (x, 10), without considering that it generate errors...(ArrayOutOfBoundsException...)
How I can make it use the data of the array when he don't started the double loop yet?
I know that it generata ArrayOutOfBoundExecption, and with a single if I can correct it like I done up 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;
}