[java] Bizzare bug
Hey,
Hopefully someone can see the glaring mistake I'm making otherwise I'll have to go back to banging my head off the wall!
I'm working on a board game and I have the board represented as a 2D array of Objects called Tiles (basically I'm drawing the board like a tiled map). Please excuse the formatting of the code.
private Tile[][] board = {
{RED_TILE, RED_TILE, RED_TILE, RED_TILE, RED_TILE, RED_TILE, RED_TILE, RED_TILE, RED_TILE},// red
{BLUE_TILE_SOUTH, BLUE_TILE, BLUE_TILE_SOUTHEAST, BLUE_TILE_SOUTH, BLUE_TILE, BLUE_TILE, BLUE_TILE, BLUE_TILE_WEST, BLUE_TILE_SOUTH}, // blue
{RED_TILE, RED_TILE, RED_TILE_SOUTH, RED_TILE_SOUTH, RED_TILE_EAST, RED_TILE, RED_TILE_SOUTH, RED_TILE_SOUTH, RED_TILE}, // red
...
};
When I assign the coordinates for each tile so it can be drawn they are calculated correctly and each Tile in the 2D array is initialized, but when I do a get to check the coordintaes they are all over the place!
There is nothing that could alter them as I don't make any other calls that access the array other than the getX and getY functions.
// tile dimensions
int tileWidth = RED_TILE.getWidth();
int tileHeight = RED_TILE.getHeight();
debug("SETTING X AND Y");
for (i = 0; i < 7; i++)
{
System.out.println("i = " + i);
for (j = 0; j < 9; j++)
{
board[j].setX(paddingX + (tileWidth * j));
board[j].setY(paddingY + (tileHeight * i));
}
}
The generated coordinates are:
0, 0
60, 0
120, 0
180, 0
240, 0
300, 0
360, 0
420, 0
480, 0
0, 60
60, 60
120, 60
180, 60
240, 60
300, 60
360, 60
420, 60
480, 60
0, 120
60, 120
120, 120
180, 120
240, 120
300, 120
360, 120
420, 120
480, 120
0, 180
60, 180
120, 180
180, 180
240, 180
300, 180
360, 180
420, 180
480, 180
0, 240
60, 240
120, 240
180, 240
240, 240
300, 240
360, 240
420, 240
480, 240
0, 300
60, 300
120, 300
180, 300
240, 300
300, 300
360, 300
420, 300
480, 300
0, 360
60, 360
120, 360
180, 360
240, 360
300, 360
360, 360
420, 360
480, 360
Now, immediately after the coordinate setting loop, I do a getX and getY, using the code below:
debug("GETTING X AND Y");
for (int k = 0; k < 7; k++)
{
for (j = 0; j < 9; j++)
{
System.out.print(board[k][j].getX() + ", ");
System.out.println(board[k][j].getY());
}
}
And I get these coordinates:
360, 300
360, 300
360, 300
360, 300
360, 300
360, 300
360, 300
360, 300
360, 300
360, 360
480, 360
120, 60
360, 360
480, 360
480, 360
480, 360
240, 360
360, 360
360, 300
360, 300
480, 300
480, 300
240, 120
360, 300
480, 300
480, 300
360, 300
480, 180
480, 180
480, 180
300, 180
480, 180
300, 180
480, 180
480, 180
480, 180
480, 360
480, 360
360, 360
360, 360
180, 360
480, 360
360, 360
360, 360
480, 360
480, 300
360, 300
120, 300
480, 300
360, 300
360, 300
360, 300
420, 300
480, 300
480, 360
480, 360
360, 360
180, 360
240, 360
360, 360
360, 360
480, 360
480, 360
Can anyone please help me with this? I'm totally out of ideas with what is causing this bug!
Is the example of the Tile[][] pseudo code? Or are all the occurences of RED_TILE, BLUE_TILE, etc, referencing a single object?
If so, that is causing the error. For example board[0][0] and board[0][1] would be the same object so:
If so, that is causing the error. For example board[0][0] and board[0][1] would be the same object so:
board[0][0].setX(0);board[0][1].setX(100);// This would print "X = 100".System.out.println("X = " + board[0][0].getX() );
OMFG! :-o
That's exactly what the problem is - Thanks Angex! It's my own fault for trying to work on Java and C versions at the same time.
In the C version I defined what a RED_TILE etc was and I totally blindly followed a similar system in the Java version by making creating an instances of the repeated Tile objects.
Eugh, while I felt very frustrated earlier on, I feel very embarrassed right now!
Thanks again, you've said my head from some wall pounding! :)
That's exactly what the problem is - Thanks Angex! It's my own fault for trying to work on Java and C versions at the same time.
In the C version I defined what a RED_TILE etc was and I totally blindly followed a similar system in the Java version by making creating an instances of the repeated Tile objects.
Eugh, while I felt very frustrated earlier on, I feel very embarrassed right now!
Thanks again, you've said my head from some wall pounding! :)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement