+ 2

How can I get my code to bounce back and forth?

The squares just go one way, stop at the end, and then shake back and forth. I’m not sure why. https://code.sololearn.com/W6yAnGFTfSac/?ref=app

6th Nov 2018, 2:53 AM
Thomas Czernek
Thomas Czernek - avatar
1 Respuesta
+ 5
You're getting stuck on the right side because when you hit the edge you start moving left, but then you're no longer at the edge so you move right again, hit the edge...move back... You may want to consider using a "direction of movement" variable, then flip its sign when you hit either side, something like this: if (hit_right_edge or hit_left_edge) direction= -direction; Notice the negative sign.
6th Nov 2018, 3:08 AM
Kirk Schafer
Kirk Schafer - avatar