|
-
Nov 10th, 2002, 08:13 AM
#1
Thread Starter
Frenzied Member
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);
-
Nov 10th, 2002, 08:20 AM
#2
Monday Morning Lunatic
Re: if statement
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).
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 10th, 2002, 08:41 AM
#3
Thread Starter
Frenzied Member
Now I can move it both ways, but it also goes outside the screen...
-
Nov 10th, 2002, 08:58 AM
#4
Monday Morning Lunatic
Perhaps altering the checks to take the size of the paddle into account?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 10th, 2002, 09:10 AM
#5
Thread Starter
Frenzied Member
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);
}
}
-
Nov 11th, 2002, 08:50 AM
#6
Code:
if (KEY_DOWN(VK_LEFT)) {
paddle.x = paddle.x -10;
if (paddle.x < 0)
paddle.x = 0;
Move_BOB(&paddle);
}
This should work.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|