May be I am tired of thinking logics (button pressing)

edited November 2013 in Programming Questions

Hi, I am trying to make a button which gets enabled when mouse is over the button and gets disable when mouse goes over the button again. Actually what's happening here is when I bring my mouse over the ellipse it keeps switching between the enable and disable mode and doesn't stop until I move my cursor. I want this to be happen once at a time.

          boolean flag=false;
          int x, y, radius=100;

          void setup() {
            size(400, 400);
            x = width/2;
            y = width/2;
          }
          void draw() {
            background(-1);
            if (dist(mouseX, mouseY, x, y)<radius/2) {
              flag=!flag;
            }
            if (flag) {
              fill(0, 255, 0);
            }
            else {
              fill(255, 0, 0);
            }
            ellipse(x, y, radius, radius);
          }

Answers

  • _vk_vk
    Answer ✓

    maybe this?

    boolean flag=false;
    boolean wasOutside = true;
    int x, y, radius=100;
    
    void setup() {
      size(400, 400);
      x = width/2;
      y = width/2;
    }
    void draw() {
      background(-1);
      if (dist(mouseX, mouseY, x, y)<radius/2 && wasOutside) {
        flag=!flag;
        wasOutside = false;
      }else if (dist(mouseX, mouseY, x, y)>radius/2){
        wasOutside = true;
      }
      if (flag) {
        fill(0, 255, 0);
      }
      else {
        fill(255, 0, 0);
      }
      ellipse(x, y, radius, radius);
    }
    
  • Exactly what I wanted ........ thanks

Sign In or Register to comment.