How Can I Make A Circle Move Back And Forth Using Conditionals, And Freezing When the Mouse is Pressed?

I am trying to make a circle move back and forth on a line in processing, however I can’t seem to get the coding right to where the circle returns back after moving to the right on the line. I’m new to coding and I’ve tried looking around for videos and info, but most of the information uses speed as a variable and not a constant and I need my speed to be a constant. How can I go about making the circle move back and forth on the line? listed below is my code.

final int X_LEFT = 100;  //The left end of the horizontal line.
final int X_RIGHT = 400; //The right end of the horizontal line.
final int Y = 100;       //The y position of the line and circle.
final int BALL_DIAM = 50;     //The diameter of the circle
final float BALL_SPEED = 2.5; //Its speed (pixels/second)

float ballX = 100;
float reverseBall;
boolean ballX = true;
boolean ballLeft = true;

void setup(){

void draw(){
  line(X_LEFT, Y, X_RIGHT, Y);

void moveBall(){
  reverseBall = -1;
  ballX = ballX + BALL_SPEED;
  if(ballX >= X_RIGHT){
   ballX = ballX +(reverseBall * BALL_SPEED);
  circle(ballX, Y, BALL_DIAM);

Hello @0_0,

This works for position:

ballX = ballX + reverseBall*BALL_SPEED;

Consider this and change direction when it exceeds your boundaries:

reverseBall *= -1;

You have 2 boundary conditions.

if you are multiplying by -1 you must initialize this properly otherwise it is always 0:

float reverseBall;

You also have a duplicate variable name that you need to comment if not used or change if you plan to use it.


@glv made a great suggestion by using reverseBall *= -1 in your conditional boundary check. This works due to the rules of multiplication.

if you have the following code:

ballX = ballX + reverseBall * BALL_SPEED;

The actual values could be:
400 = 400 + (-1 * 2.5)
400 = 400 + (-2.5)

This will make the ball move to the left as you are subtracting 2.5 from ballX position within each loop. When your X_LEFT condition is triggered you want to somehow get a positive 2.5 instead of the previous -2.5. Given that your BALL_SPEED is a constant the only thing you can change is the reverseBall value in the equation by doing:

reverseBall *= -1

-1 *= -1 which is equal to positive 1 as a negative times a negative is a positive. So now when you go back to your ball movement the equation is the same but with the reverseBall value now being different:

ballX = ballX + reverseBall * BALL_SPEED;

The actual values would be:
100 = 100 + (1 * 2.5)
100 = 100 + (2.5)

Now you are adding 2.5 to the ballX position within each loop. Now you just need to check the X_RIGHT condition and when it is exceed you run the same reverseBall *= -1 code so that the value of reverseBall is -1 resulting in -2.5 when the ball attempts to move again.

Now for getting it to stop or freeze when the mouse is pressed. If we know that the code that is making the ball move is:

ballX = ballX + reverseBall * BALL_SPEED;

We essentially need this code to run but to be bypassed when the mouse is being pressed. You can use the mousePressed function to accomplish this.

1 Like