【问题标题】:Making a moving circle disappear after being clicked on, Processing使移动的圆圈被点击后消失,处理
【发布时间】:2016-12-05 03:27:47
【问题描述】:

我编写了一个程序,其中一个 UFO(本质上是一个灰色椭圆)从屏幕中心出现并飞到边缘。鼠标按下时会出现激光,松开鼠标时会消失。我想让它在鼠标点击它/激光触摸它时消失。 我已经完成了制作 UFO 类并创建了决定其运动和速度的变量,并且我能够让激光直接出现在光标上。我想制作一个“if”语句来检查光标是否在 UFO 的半径(或直径)内,并将其放在我为 UFO 创建的 for 循环内。但是,我不确定如何实现正确的语法来实现它。 注意:播放草图后,您可能需要等待几秒钟才能出现第一个圆圈。

    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);
      }
      }
    }

【问题讨论】:

  • 请在交叉帖子之间链接:forum.happycoding.io/t/… 看起来你删除了我已经在 Stack Overflow 上回答的问题。这个问题去哪儿了?

标签: java processing processing.js


【解决方案1】:

就像我在the Happy Coding forum说的:

基本上,如果你的UFO是一系列圆,那么你只需要使用dist()函数来检查鼠标到圆心的距离是否小于圆的半径。如果是,则鼠标在圆圈内。这是一个小例子:

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;
 }
}

如果您的 UFO 是多个圆圈,那么您需要将此逻辑应用于每个圆圈。如果您遇到困难,请尝试一下并发布一个像这样的小示例(不是您的整个草图)。祝你好运。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-04
    • 1970-01-01
    相关资源
    最近更新 更多