2016-12-05 107 views
1

我寫了一個程序,其中一個UFO(實質上是一個灰色橢圓)從屏幕中心出現並飛向邊緣。當按下鼠標時會出現激光,並在釋放鼠標時消失。我想做的是當鼠標點擊/激光觸摸它時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); 
     } 
     } 
    } 
+0

請鏈接crossposts:http://forum.happycoding.io/t/make-a-moving-circle-disappear-when-clicked-on/47 它看起來像你刪除了我回答的問題這已經在堆棧溢出。那個問題去了哪裏? –

回答

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是多個圓圈,那麼你需要將這個邏輯應用到每個圓圈。請嘗試一下併發佈一個像這樣的小例子(不是你的整個草圖),如果你卡住了。祝你好運。

相關問題