Page 1 of 1

Help with Bounce

Posted: Fri Dec 14, 2007 10:50 pm
by samnicole
So I am making a game and I need to figure out how to get my ball to bounce off the walls. My wall stops at 450 and I am in screen 9. Any help would be appreciated. I had bx = bx + 5 for the speed and had an if then statement.

if bx <=450 then
bx = bx - 5

and it stops the ball , but i can't quite figure out the code for the bounce.

Posted: Fri Dec 14, 2007 11:07 pm
by Nodtveidt
One way to do it is to have a separate variable that will be used for incrementing the ball's position.

Code: Select all

DIM balldirx AS INTEGER
balldirx = 5
... code here ...
bx = bx + balldirx
IF bx> 449 OR bx < 1 THEN balldirx = -balldirx
Essentially, you are using this second variable to help move the ball and also to control its direction...when you use the -balldirx what you're doing is reversing its sign, so if it was 5 it would become -5 and vice versa.

Make sense? :D

Posted: Fri Dec 14, 2007 11:09 pm
by coma8coma1
i would imagine you could have a horizontal velocity variable. like lets say it's "xvel"

it starts out being 5 and then when the conditions are right it gets set to -5

then each frame you just have bx = bx + xvel and update the screen

Posted: Fri Dec 14, 2007 11:09 pm
by coma8coma1
yeah like nodtveit said lol ;) :oops: