2016-12-05 2 views
1

UFO (본질적으로 회색 타원)가 화면 가운데에서 나타나고 가장자리로 날아 오는 프로그램을 작성했습니다. 마우스를 누르면 레이저가 나타나고 마우스를 놓으면 사라집니다. 마우스가 클릭 할 때 UFO가 사라 지도록/레이저가 닿도록 UFO를 만들고 싶습니다. 나는 UFO 클래스를 만들고 그것의 움직임과 속도를 결정하는 변수를 생성하기 위해 그것을 만들었고 레이저를 커서에 직접 나타나게 할 수있었습니다. 커서가 UFO의 반경 (또는 지름) 내에 있는지 확인하고 UFO를 위해 만든 for 루프 안에 커서를 놓는 'if'문을 만들려고했습니다. 그러나, 나는 그것이 일어날 수 있도록 적절한 문법을 ​​성취하는 방법을 확신하지 못한다. 참고 : 스케치를 재생 한 후 첫 번째 원이 나타나려면 몇 초 정도 기다려야합니다. I처럼클릭하면 움직이는 서클이 사라집니다. Processing

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

크로스 링크 사이를 연결하십시오. http://forum.happycoding.io/t/make-a-moving-circle-disappear-when-clicked-on/47 그리고 내가 대답 한 곳의 질문을 삭제 한 것 같습니다. 이 이미 스택 오버플로에. 그 질문은 어디에서 났습니까? –

답변

1

the Happy Coding forum에 말했다 : 당신의 UFO가 원 일련의 경우

기본적으로, 당신은 단지의 중심에 마우스에서의 거리 여부를 확인하기 위해 dist() 기능을 사용할 필요가 circle은 원의 반지름보다 작습니다. 맞으면 마우스가 원 안에 있습니다. 다음은 작은 예입니다.

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가 여러 개의 서클 인 경우이 논리를 각 원에 적용해야합니다. 을 시도하고 붙어 있다면이 작은 그림을 게시하십시오 (전체 스케치가 아님). 행운을 빕니다.