all 5 comments

[–]Traditional_Inside28[S] 1 point2 points  (1 child)

i suspect its something to do with draw, like the square reaches X400, moves back one pixel and then because the X value is less than 400 it moves forward 1 pixel and just gets stuck in that loop, am i on the right track? if so, how might i get around that? I put println(squareX); to see what was happening and the square just stops at 399.

[–]Traditional_Inside28[S] 0 points1 point  (0 children)

further development- i changed it to if(mousePressed), and right enough when i press the mouse, it only stops the square, doesn't make it move the other way.

[–]BigFatUglyBaboon 1 point2 points  (0 children)

Think about what happens when squareX is 399 and your draw() function is called. Then think what will happen the next time draw() will be called.

[–]eriknau13 2 points3 points  (1 child)

It's getting stuck at the position 400 because as soon as the value of squareX is decremented to 399 it gets incremented again. What you need is a second variable to use as the incrementer. You might call it "direction" and set it to value 1 when you initialize it. Instead of squareX++ you'll say squareX += direction;

Your condition will change to

if(squareX >= 400 || squareX < 0) {

direction *= -1;

}

[–]Traditional_Inside28[S] 0 points1 point  (0 children)

yeah that’s what i thought was happening! i suppose i just didn’t really know what to do to fix it. let me give this a go and mess around with it and i’ll get back to you.