Make a moving circle disappear when clicked on


#1

I have written a program in which a UFO (in essence, a gray ellipse) appears from the center of the screen and flies to the edge. There is a laser that appears when the mouse is pressed, and disappears when the mouse is released. I want to make it so that the UFO disappears when the mouse clicks on it/the laser touches it.
I’ve made it as far as to make the UFO class and create variables that determine its movements and speed, and I was able to get the laser to appear directly on the cursor. I thought of making an ‘if’ statement to check if the cursor is within the radius (or diameter) of the UFO, and placing it inside of the for loop I created for the UFOs. However, I am not sure how to achieve the proper syntax to make it happen.
Note: You may need to wait a few seconds for the first circle to appear after you play the sketch.

    UFO[] ufos = new UFO[3];
    
    void setup() {
      size(700, 700);
      for (int j = 0; j < ufos.length; j++) {
          ufos[j] = new UFO();
      }
    }
    
    //UFO class
    //Class setup ends on line 61
    class UFO {
     float a;
     float b;
     float c;
     float sa;
     float sb;
     float d;
    
     UFO() {
       //declare float a/b/c value
      a = random(-width/2, width/2);
      b = random(-height/2, width/2);
      c = random(width);
     }
     //UFO movement
     void update() {
       //float c will serve as a speed determinant of UFOs
      c = c - 1;
     if (c < 5) {
       c = width;
     }
     }
     
     //UFO setup
     void show() {
       
       //moving x/y coordinates of UFO
       float sa = map(a / c, 0, 1, 0, width);
       float sb = map(b / c, 0, 1, 0, height);
       float d = map(c, 0, width, 50, 0);
      
      //UFO drawing shapes
      //ellipse is always sa (or sb) / c to allow UFO to appear 
      //as if it is moving through space
    fill(200);
    ellipse((sa / c), (sb / c), d + 5, d+5);
    
    
    //Hides UFO way off the screen
    //and replaces it with a black-filled ellipse until
    //it forms into a full circle
    //When radius d becomes 50, the UFO flies from the
    //center of the screen to off of the screen
     if (d < 50) {
         fill(0);
         ellipse(-5, -10, 90, 90);
        sa = 10000;
        sb = 10000;
        
       }
     }
    } 
    
    
    void draw() {
      //Background
      background(0);
      //Translated the screen so that the UFOs appear to fly from
      //the center of the screen
      translate(width/2, height/2);
      
      //UFO draw loop, make UFO visible on screen
      for (int j = 0; j < ufos.length; j++) {
        ufos[j].update();
        ufos[j].show();
        
         //mouse-click laser
      if (mousePressed == true) {
        fill(200,0,0);
        ellipse(mouseX - 352,mouseY - 347,50,50);
      }
      }
    }

#2

Please link between crossposts: http://stackoverflow.com/questions/40966663/making-a-moving-circle-disappear-after-being-clicked-on-processing

And it looks like you deleted the question where I answered this already on Stack Overflow. Where did that question go?

But basically, if your UFO is a series of circles, then you just need to use the dist() function to check whether the distance from the mouse to the center of the circle is less than the radius of the circle. If it is, then the mouse is inside the circle. Here’s a small example:

float circleX = 50;
float circleY = 50;
float circleDiameter = 20;
boolean showCircle = true;

void draw(){
  background(0);
  if(showCircle){
   ellipse(circleX, circleY, circleDiameter, circleDiameter); 
  }
}

void mousePressed(){
 if(dist(mouseX, mouseY, circleX, circleY) < circleDiameter/2){
   showCircle = false;
 }
}

If your UFO is multiple circles, then you need to apply this logic to each circle. Please try something and post a small example like this one (not your whole sketch) if you get stuck. Good luck.