Problem with missing ball (foo)

I have a sketch (see below) where a ball moves. When I tested it, everything seems to work but now the ball has gone off the screen to the right somewhere. How do I get my ball back?

Ball[] balls = new Ball[1];


void setup() {
    size(400, 400);
  
balls[0] = new Ball(40, 127); // diameter and colour
}

void draw() {
    background(255);
  for (int i = 0; i < balls.length; i++) {
    balls[i].display();
    balls[i].move();

  }

  
}

class Ball {
  float dia;
  float x;
  float y; 
  float col;
  float xspeed;
  float yspeed;
  float r;

  Ball (float tempD, float tempC) {
    x = random(0+dia, width-dia);
    y = random(0+dia, height-dia);
    dia = tempD;
    col = tempC;
    r = dia/2;
    xspeed = 1.0;
    yspeed = 0.0;
  }

  void display() {
    noStroke();
    fill(col);
    ellipse(x, y, dia, dia);
  }

  void move() {

    x = x + xspeed;
    y = y + yspeed;
  }


}

You can check for the screen border and then reverse the direction of the movement

see https://www.processing.org/examples/bounce.html

Remark

color is not really float. Either use int or even color.

see https://www.processing.org/reference/color_datatype.html

1 Like

Thanks. This was my very poor attempt to make an April’s fool joke. I liked the idea that someone could be foolish enough to try to get their ball back. Looks like the joke is on me!

Noted about color not being a float.

1 Like

Hey! How do you say?

You sent me in the April! :wink:

I like it!!!

I was about to write restart your Sketch!

To get your ball back.

I am the April fool… :wink:

1 Like

I almost answered this post too!
:slight_smile:

2 Likes

Next year @debxyz :wink:

1 Like

LOL
I’ll mark my calendar.
:grinning:

2 Likes

:wink:

Warm regards

Chrisir

2 Likes