I have a player, that I can control using WASD. And I don't want him to leave the screen.
So I've tried the following:
if (player1.getX()+5 < 974 && player1.getX()-5 > 0) {
if(Keyboard.isKeyDown(Input.KEY_D)) {
player1.addForce(5, "d");
}
if(Keyboard.isKeyDown(Input.KEY_A)) {
player1.addForce(5, "a");
}
if(Keyboard.isKeyDown(Input.KEY_W)) {
player1.addForce(5, "w");
}
if(Keyboard.isKeyDown(Input.KEY_S)) {
player1.addForce(5, "s");
}
} else if(player1.getX() >= 974) {
player1.addForce(5, "a");
} else if(player1.getX() <= 0) {
player1.addForce(5, "d");
}
It actually works, but the player gets stuck at the edge of screen, and can't move anymore. Any ideas why? Or rather, how do I make a proper collision detection?
NB: 1024 is the width of screen. And my player is 50 pixels wide, thus the 974. I am only working with X at the momemnt, to try getting it to work.