0

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;
    }
dontHaveName
  • 1,899
  • 5
  • 30
  • 54
  • it doesn't have anything with that link – dontHaveName Mar 24 '15 at 15:49
  • Try splitting up your collision() into collisionRight(), collisionUp(), etc. – Manu Mar 24 '15 at 16:00
  • @Manu, yes already tried to do that, but I do not know exactly how to detect in which way is it colliding – dontHaveName Mar 24 '15 at 16:05
  • Could you show us the collision method then? – Manu Mar 24 '15 at 16:07
  • I think you should split up your movement function, depending on the number of arrows pressed. 1) 1 arrow: move + move back in case of collision 2) 2 arrows: move + move back in case of collision if collided, try move in direction 1 if collided again, try to move in direction 2 3) 3 arrows: detect this case before moving and reduce it to 1 arrow (up+down+right = right) – Manu Mar 24 '15 at 17:38

0 Answers0