Friday, 27 September 2013

Collision detection in tile-base game only applies to the file tile in the array

Collision detection in tile-base game only applies to the file tile in the
array

I've written a prototype in java that employs a movable character and
tile-based graphics and game logic. The layout of each level is stored in
a 10x10 2D array. When I use the following code to detect whether or not
the player is colliding with a '1' (empty) tile, it only returns positive
when the player is colliding with the final '1' tile in the array, why is
this? I understand this method is incredibly inefficient.
for(int r = 0; r < levArray.length; r++) {
for(int c = 0; c < levArray[0].length; c++) {
if(levArray[r][c] == 1) {
if(px >= r*64-9 && px <= (r+1)*64+11 && py >= c*64-30 &&
py <= (c+1)*64+30) {
isColliding = true;
} else {
isColliding = false;
}
}
}
}

No comments:

Post a Comment