0

I'm using Processing; I have a ball that bounces when it hits the border and changes its color to random. Now I need this ball to change its color on every third bounce. Can't figure out how to do it.

Here is my current code:

float xPos;// x-position
float vx;// speed in x-direction
float yPos;// y-position
float vy;// speed in y-direction
float r;
float g;
float b;

void setup()
{
    size(400, 300);
    fill(255, 177, 8);
    textSize(48);

    // Initialise xPos to center of sketch
    xPos = width / 2;
    // Set speed in x-direction to -2 (moving left)
    vx = -2;
    yPos = height / 2;
    vy = -1;
}

void draw()
{
    r = random(255);
    b = random(255);
    g = random(255);

    background(64);

    yPos = yPos + vy;
    // Change x-position on each redraw
    xPos = xPos + vx;

    ellipse(xPos, yPos, 50, 50);
    if (xPos <= 0)
    {
        vx = 2;
        fill(r, g, b);
    } else if (xPos >= 400)
    {
        vx = -2;
        fill(r, g, b);
    }
    if (yPos <= 0)
    {
        vy = 1;
        fill(r, g, b);
    } else if (yPos >= 300)
    {
        vy = -1;
        fill(r, g, b);
    }    
}
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
  • 1
    Welcome to SO. Please visit [help] and read [ask]. Without seeing your code, how can we answer? – 001 Oct 04 '17 at 15:41
  • Well, you maintain a counter as `int counter = 0` or something like that which you increase every time it bounces (`counter++;`). When it reaches `3` like `counter == 3` you change the color, use an `if`-statement therefore. After that you reset the counter: `counter = 0`. In order to give further help you **need to provide** us the relevant parts of your **code**. – Zabuzard Oct 04 '17 at 15:42
  • This sounds like something you can achieve using the modulus operator (%), example: https://stackoverflow.com/questions/9008522/insert-tr-after-every-third-loop – Mr. Greenwoodz Oct 04 '17 at 15:42
  • 1
    I'm voting to close this question as off-topic because it's not a real question, just a requirements dump. – EJoshuaS - Stand with Ukraine Oct 04 '17 at 15:44
  • https://pastebin.com/5jgQFpM9 This is the code. I'm newbie at programming so don't be so mean :p – Ivan Silvestrov Oct 04 '17 at 15:46
  • 1
    @IvanSilvestrov It is quite likely that the question will be closed if you do not edit and improve the question fast. If that is the case you can of course re-create the question, but with improved content. Therefore please carefully read [How to ask](https://stackoverflow.com/questions/how-to-ask) before, thanks. – Zabuzard Oct 04 '17 at 15:47
  • Don't link to the code off-site - include a [mcve] in the question itself. – EJoshuaS - Stand with Ukraine Oct 04 '17 at 15:48
  • Thanks, I'm new to the site :D sorry for rushing, wanted to complete it fast! – Ivan Silvestrov Oct 04 '17 at 15:49

1 Answers1

1

It is quite easy. You maintain a counter which counts the amount of bounces. Therefore you increase the counter by one after every bounce. If it reaches 3 you change the color. After that you reset the counter and repeat.


Therefore add this member variable to your class (like you already did with xPos and others):

private int bounceCounter = 0;

which introduces the variable bounceCounter initially holding 0 as value.

Here is the modified draw method with highlighted changes and comments:

void draw() {
    // New color to use if ball bounces
    r = random(255);
    b = random(255);
    g = random(255);

    background(64);

    yPos = yPos + vy;
    // Change x-position on each redraw
    xPos = xPos + vx;

    ellipse(xPos, yPos, 50, 50);

    // Variable indicating whether the ball bounced or not
    boolean bounced = false;

    // Out of bounds: left
    if (xPos <= 0) {
        vx = 2;
        bounced = true;
    // Out of bounds: right
    } else if (xPos >= 400) {
        vx = -2;
        bounced = true;
    }

    // Out of bounds: bottom
    if (yPos <= 0) {
        vy = 1;
        bounced = true;
    // Out of bounds: top
    } else if (yPos >= 300) {
        vy = -1;
        bounced = true;
    }

    // React to bounce if bounced
    if (bounced) {
        // Increase bounce-counter by one
        bounceCounter++;

        // Third bounce occurred
        if (bounceCounter == 3) {
            // Change the color
            fill(r, g, b);

            // Reset the counter
            bounceCounter = 0;
        }
    }
}
Zabuzard
  • 25,064
  • 8
  • 58
  • 82