Using boolean functions in object oriented programming

Hey everyone. I'm extremely new to processing, but am trying to make a ball that blinks when you click on it. It's required that this function returns true or false but I don't know how to use boolean functions in OOP. It seems that I've tried everything. This is what I have so far. How do I implement this in setup() or draw()? Help!

boolean Clicked (float x, float y, float distance){
  distance = sq((mouseX - x)) + sq((mouseY - y);
  if (distance <= radius*radius) 
    // make the ball that was click on blink
    // if clicked on again stop blinking
}
Tagged:

Answers

  • Answer ✓

    //make the ball that was click on blink //if clicked on again, stop blinking

    This concept does not agree with what the function returns. Either the function does something (make a previously defined ball blink - ball being a member object of your class) which will not required to return a boolean value. On the other hand, if it returns a boolean value, some other part of the code will handle the blinking. You are better off if you post all your code, or even better, a small sample code showing your concept and what you have so far. You can also check the following example showing a button class. The code demonstrates hover action over the button as well as click action. This example might be relevant to your request.

    Kf

    class Button {
      String label;
      float x;      
      float y;    
      float w;    
      float h;    
    
      // constructor
      Button(String labelB, float xpos, float ypos, float widthB, float heightB) {
        label = labelB;
        x = xpos;
        y = ypos;
        w = widthB;
        h = heightB;
      }
    
      void Draw() {
        fill(218);
        stroke(141);
        rect(x, y, w, h, 10);
        textAlign(CENTER, CENTER);
        fill(0);
        text(label, x + (w / 2), y + (h / 2));
      }
    
      boolean MouseIsOver() {
        if (mouseX > x && mouseX < (x + w) && mouseY > y && mouseY < (y + h)) {
          return true;
        }
        return false;
      }
    }
    
    
    
    
    Button b1;
    String onClickStr="";
    void setup() {
      size(600, 400);
      rectMode(CORNER);
      textAlign(CENTER, CENTER);
      b1=new Button("Tap me!", width*0.2, 200, width*0.6, 50);
    }
    
    void draw() {
      background(92);
      b1.Draw();
    
      if (b1.MouseIsOver()==true) {
        text("On button", width/2.0, height*0.75);
      } else {
        text("Away from button", width/2.0, height*0.75);
      }  
    
      text(onClickStr, width/2.0, height*0.9);
    }
    
    void mouseReleased() {
      if (b1.MouseIsOver()==true) {
        onClickStr="Clicked on Button!!!";
      } else {
        onClickStr="Clicked outside of button";
      }
    }
    
Sign In or Register to comment.