1

I'm trying to make my character move around a tile map with collisions. Everything works fine except for one thing. I show you a picture with the problem:

http://i.imgur.com/bcyz5.jpg That is, when I reach a tile above then I can not move anywhere. If you come from the left, I can not move either up or down. If you reach the bottom, I can move to the left but not right. And when you reach the right I can move in any direction.

Honestly I have no idea what may be failing. I think it has to do with if (...), because if I change the order, the addresses where I can move change :/

Here I leave some code:

boolean collision = false;

if(Keyboard.isKeyDown(Keyboard.KEY_UP)) {
    for(int i = 0; i < map.GetNumLayers(); i++) {
        if(UpTile(map, i) > 128) {
            collision = true;
        }
    }

    if(!collision) AddPos(0.0f, -vel);
}
if(Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
    for(int i = 0; i < map.GetNumLayers(); i++) {
        if(LeftTile(map, i) > 128) {
            collision = true;
        }
    }

    if(!collision) AddPos(-vel, 0.0f);
}
if(Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
    for(int i = 0; i < map.GetNumLayers(); i++) {
        if(DownTile(map, i) > 128) {
            collision = true;
        }
    }

    if(!collision) AddPos(0.0f, vel);
}
if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
    for(int i = 0; i < map.GetNumLayers(); i++) {
        if(RightTile(map, i) > 128) {
            collision = true;
        }
    }

    if(!collision) AddPos(vel, 0.0f);
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
Kactung
  • 690
  • 2
  • 8
  • 21

1 Answers1

1

This will be easier of you separate your game's model from the view shown above. This example shows one approach, while this more elaborate example models a related grid based game.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045