-
if statement
I'm using the following if statement in my game, but I can only move the player to the left, not to the right....what is wrong??
PHP Code:
if (KEY_DOWN(VK_LEFT))
if (!paddle.x <=0)
paddle.x = paddle.x -10;
Move_BOB(&paddle);
if(KEY_DOWN(VK_RIGHT))
if(!paddle.x > SCREEN_WIDTH)
paddle.x = paddle.x +10;
Move_BOB(&paddle);
-
Re: if statement
Quote:
Originally posted by CyberCarsten
I'm using the following if statement in my game, but I can only move the player to the left, not to the right....what is wrong??
PHP Code:
if (KEY_DOWN(VK_LEFT))
if (!paddle.x <=0)
paddle.x = paddle.x -10;
Move_BOB(&paddle);
if(KEY_DOWN(VK_RIGHT))
if(!paddle.x > SCREEN_WIDTH)
paddle.x = paddle.x +10;
Move_BOB(&paddle);
Some judicious insertion of braces later:
Code:
if (KEY_DOWN(VK_LEFT)) {
if (paddle.x > 0) {
paddle.x = paddle.x -10;
Move_BOB(&paddle);
}
} else if(KEY_DOWN(VK_RIGHT)) {
if(paddle.x < SCREEN_WIDTH) {
paddle.x = paddle.x +10;
Move_BOB(&paddle);
}
}
If you miss out the braces it only takes the next statement, meaning that in your first, the Move_BOB would be executed whether it was VK_LEFT or any other.
The modified one should work...
Edit: I also changed your comparison expressions, they were comparing a boolean value to a number (precedence of the ! operator).
-
Now I can move it both ways, but it also goes outside the screen...
-
Perhaps altering the checks to take the size of the paddle into account?
-
I changed your suggestion for the right movement and I got it to stop when it reached the right end of the screen, but when I press Left it just continues out to the left of the screen...
PHP Code:
if (KEY_DOWN(VK_LEFT)) {
if (paddle.x > 0) {
paddle.x = paddle.x -10;
Move_BOB(&paddle);
}
} else if(KEY_DOWN(VK_RIGHT)) {
if(paddle.x + paddle.width < SCREEN_WIDTH) {
paddle.x = paddle.x +10;
Move_BOB(&paddle);
}
}
-
Code:
if (KEY_DOWN(VK_LEFT)) {
paddle.x = paddle.x -10;
if (paddle.x < 0)
paddle.x = 0;
Move_BOB(&paddle);
}
This should work.