Button bug

FluFlu
edited June 2014 in Questions about Code

So I have this code:

    float scale = 0;

    void setup() {
      size(200, 200);
    }

void draw() {
  background(255);
  buttonEllipse(width/2, height/2, 20, color(178, 255, 0), color(41, 255, 0), true);
}

void buttonEllipse(int posX, int posY, int size, color colorOut, color colorIn, boolean function) {
  if (function == true) {
    int counter = 1;
    fill(colorOut);
    stroke(150);
    ellipse(posX, posY, size, size);

    if (mouseX >= posX - size/2 && mouseX <= posX + size/2 && mouseY >= posY - size/2 && mouseY <= posY + size/2) {
      fill(colorIn);
      stroke(150);
      ellipse(posX, posY, scale, scale);

      scale += 4;
      if (scale == 20) {
        scale = 20;
      }
    } else {
      scale = 0;
    }
  } else {
    loop();
  }
}

What I'm trying to do is when scale hits 20, it should stop there, and when the mouse goes off the button, reset the value to 0 again, but for some reason, it doesn't stop there, and it rises... Somebody can help me fix this huge bug please?

Tagged:

Answers

  •   scale += 4;
      if (scale == 20) {
        scale = 20;
      }
    

    so, if scale is 20 you're setting it equal to 20?

    what if it's 24?

  • Answer ✓

    This is safer: scale = min(scale + 4, 20); =:)

  • Thank you GoToLoop, you are always solving my problems!

Sign In or Register to comment.