Randomize color on bouncing ball

I’ve created a bouncing ball and want to randomize the color of the ball with each hit against the size perimeter. However, with the following code it will only randomize the color once and stay that color (filler is the randomized color).

if (xpos > width-rad || xpos < rad || ypos > height-rad || ypos < rad) {
    fill(filler);
}

I’m pretty sure it has something to do with the fact that it has fulfilled one of the conditions and therefor remains that one color. I just don’t know how to work around it. I’ve tried with “else if” for each of the conditions, but that does not work either.

If you call fill() but don’t change your variable, it will always fill with that color. So just put something like

filler = color(int(random(256)), int(random(256)), int(random(256)));

at your fill() statement.

1 Like