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:
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);
}