I have mya player, that iI can control using WASD. And what iI don't want him to be able to. Is to leave the screen. So i've tried following, actually work, but the player gets stuck at the edge of screen, and can't move anymore.
EDITSo I've tried the following: 1024 to 974 because my player1 is 50 wide
if (player1.getX()+5 < 974 && player1.getX()-5 > 0) {
//Player 1
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 iI make a proper collision detection with movement?
InfoNB: 1024 is the width of screen. And my player is 50 pixels wide, thus the 974. I am only working with X atmat the momemnt, to try getting it to work.