I have no idea of doing it!
in the begining of the tick method in the player i set xPrev and yPrev to x,y.
Then i do the moving… If intersects then i set x,y to xPrev and yPrev. I get stuck in one place pls help!
public void move() {
xPrev = x;
yPrev = y;
if (Controler.up) {
y -= Game.pSpeed;
dir = 1;
animUp.startAnimation();
}
if (Controler.down) {
y += Game.pSpeed;
dir = 0;
animDown.startAnimation();
}
if (Controler.left) {
x -= Game.pSpeed;
dir = 3;
animLeft.startAnimation();
}
if (Controler.right) {
x += Game.pSpeed;
dir = 2;
animRight.startAnimation();
}
colDect();
}
private void colDect() {
for (int i = 0; i < world.tiles.length; i++) {
for (int j = 0; j < world.tiles[0].length; j++) {
if (colBox.intersects(world.tiles[i][j]) && world.tiles[i][j].solid) {
x = xPrev;
y = yPrev;
}
}
}
}
Edit:
What i did is that i moved the collision box first and checked if the is a collision if it was colliding i wouldn’t move! But i get it so that my player can’t move if i go left and up and there is collision up!For now it’s good i guess!