0

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;
}
aziis98
  • 81
  • 2
  • 7

2 Answers2

0
int Water = 1;
int Air = 0;
int[][] map = new int[20][20];

void update()
{   
    for(int x = 0; x < 10; x++)
    {
        for(int y = 9; y >= 0; y--)
        {
            tick(x, y, map);
        }
    }
}

void tick(int x, int y, int[][] m)
{
    if (y < m[0].length - 1)
        m[x][y+1] = m[x][y];
}

Of course, you'll need to make some cells water to begin with, but that should go in the constructor.

nullptr
  • 2,244
  • 1
  • 15
  • 22
  • I know that it generate that error, I mean that if you try that with your correction it'll put in a single cicle the "1" at the bottom.. – aziis98 May 02 '13 at 19:23
  • Can you clarify what you mean? I'm not sure what you're trying to do here. – nullptr May 02 '13 at 19:26
  • Still not sure. Try posting your complete code and stating what's not working properly. – nullptr May 02 '13 at 20:06
  • Please post your code -- I'm afraid I just can't understand what you're trying to say. – nullptr May 02 '13 at 20:22
0

You are saying that you are iterating your map row by row when you are actually doing it by columns. Try looping first for y and then for x.

Your update method is also wrong. y+1 when y = 9 will try to access map[x][10] which will throw an ArrayOutOfBoundsException. Remember than an array declared as new int[10] has 10 items starting from 0 and ending at position 9.

Alex
  • 1,066
  • 9
  • 13
  • And what if I want to also get data from x - 1 or x + 1? – aziis98 May 02 '13 at 20:41
  • If you want us to understand what you are trying to achieve, create two array examples showing the input and the expected output. – Alex May 02 '13 at 20:53