I'm trying to make classic bomberman game, so I have character image and tiles(blocks). I'm detecting collision with method collision
which is returning true when character intersects with some tile(block).
Character movement looks like this:
public void moveUp() {
yMove = -SPEED;
}// and other similar to other directions
public void update() {// this is called in cyclus
y += yMove;
x += xMove;
if (collision() == true) {
if (isMovingUp()) {
y -= yMove;
}
if (isMovingDown()) {
y += -yMove;
}
if (isMovingLeft()) {
x -= xMove;
}
if (isMovingRight()) {
x += -xMove;
}
}
}
This works if am I using just 1 arrow. If I press 2 arrows(e.g. I'm pressing down and right, I can go down but I can't go the right, character is staying at current position -> it should move down), if I press 3 arrows(left,right,down) I can even go through the wall..
Any ideas? Thanks.
Edit: collision method:
public boolean collision() {
playerRect = new Rectangle(x, y, PLAYER_WIDTH, PLAYER_HEIGHT);
for(int i = 0; i < map.length; i++) {
for(int j = 0; j < map[0].length; j++) {
if(map[j][i].getType() >= 1) {
Rectangle tileRect = new Rectangle(i*32, j*32, 32, 32);
rect.add(tileRect);
}
}
}
for(Rectangle collision : rect) {
if(collision.intersects(playerRect)) {
return true;
}
}
return false;
}