2016-10-09 2 views
0

원이 화면 상단에서 하단으로 떨어지는 게임을 만듭니다. 원이 클릭되면 화면 상단의 임의의 위치와 임의의 색상으로 다시 스폰한다고 가정합니다. 마우스 클릭이 원 중 하나에 있는지 또는 올바르게 작동하는지 확인하기 위해 내 문제가 내 라인과 관련이 있다는 것을 확신합니다. 그래서 내 질문은 마우스 클릭이 서클 중 하나에서 또는 배경 화면에서 발생했는지 어떻게 결정할 수 있습니까? 다음 줄에 무엇이 잘못 되었습니까?JavaFx 마우스가 배경 또는 서클을 클릭하는지 확인

if((shape.get(i).getLayoutX() == e.getX())&&(shape.get(i).getLayoutY() == e.getY())){ 

내 전체 코드는 여기 (내 문제는 그 줄에서 것을 거의 확신 때문에) :

public class ShapesWindow extends Application{ 
final int WIDTH = 640; 
final int HEIGHT = WIDTH/12 * 9; 
Random r = new Random(); 
Circle circle; 
double yCord; 
long startNanoTime; 

Group root = new Group(); 
Scene scene = new Scene(root, WIDTH, HEIGHT); 
Canvas can = new Canvas(WIDTH,HEIGHT); 
GraphicsContext gc = can.getGraphicsContext2D(); 
ArrayList<Shape> shape = new ArrayList<>(); 

@Override 
public void start(Stage theStage) throws Exception { 
    theStage.setTitle("Click the bubbles!"); 
    theStage.setScene(scene); 
    root.getChildren().add(can); 
    gc.setFill(Color.LIGHTBLUE); 
    gc.fillRect(0,0,WIDTH,HEIGHT); 

    /* This adds 10 circles to my Group */ 
    for(int i = 0; i < 10; i++){ 
     gc.setFill(Color.LIGHTBLUE); 
     gc.fillRect(0,0,WIDTH,HEIGHT); 
     circle = new Circle(15,randomColor()); 
     root.getChildren().add(circle); 
     circle.setLayoutX(r.nextInt(WIDTH+15)); 
     circle.setLayoutY(0); 
     shape.add(circle); 
    } 

    /* This my attempt at trying to handle the Mouse Events for each thing */ 
    for(int i = 0; i < 10; i++){ 
     shape.get(i).setOnMouseClicked(
       new EventHandler<MouseEvent>(){ 
        public void handle(MouseEvent e){ 
         shapeClicked(e); 

        } 
       }); 
    } 

    startNanoTime = System.nanoTime(); 
    new AnimationTimer(){ 
     public void handle(long currentNanoTime){ 
      double t = (currentNanoTime - startNanoTime)/1000000000.0; 
      yCord = t*20; 

      for(int i = 0; i < 10; i++){ 
       /* This if statment allows nodes to wrap around from bottom to top */ 
       if(yCord >=HEIGHT){ 
        shape.get(i).setLayoutX(r.nextInt(WIDTH+15)); 
        shape.get(i).setLayoutY(0); 
        shape.get(i).setFill(randomColor()); 
        resetNan(); 
       } 

       shape.get(i).setLayoutY(yCord); 
      } 
     } 
    }.start(); 
    theStage.show(); 
} 

/* 
    * This Function is suppose the change the color and position of the circle that was clicked 
    */ 
public void shapeClicked(MouseEvent e){ 
    for(int i = 0; i < shape.size();i++){ 
     if((shape.get(i).getLayoutX() == e.getX())&&(shape.get(i).getLayoutY() == e.getY())){ 
      shape.get(i).setLayoutX(r.nextInt(WIDTH+15)); 
      shape.get(i).setLayoutY(0); 
      shape.get(i).setFill(randomColor()); 
    } 
} 

/* 
    * This allows the value of startNanoTime to be indrectly change it can not be changed diretly 
    * inside of handle() inside of the Animation class 
    */ 
public void resetNan(){ 
    startNanoTime = System.nanoTime(); 
} 

public Color randomColor(){ 
    double R = r.nextDouble(); 
    double G = r.nextDouble(); 
    double B = r.nextDouble(); 
    double opacity = .6; 
    Color color = new Color(R, G, B, opacity); 
    return color.brighter(); 
} 
public static void main(String[] args){ 
    launch(args); 
} 

} 

답변

0

왜 그냥

for(int i = 0; i < 10; i++){ 
    Shape s = shape.get(i); 
    s.setOnMouseClicked(
      new EventHandler<MouseEvent>(){ 
       public void handle(MouseEvent e){ 
        s.setLayoutX(r.nextInt(WIDTH+15)); 
        s.setLayoutY(0); 
        s.setFill(randomColor()); 
       } 
      }); 
} 
+1

그것은'layoutX를 사용하는 것이 보통이 아니다 '및'layoutY'를 사용하여 'Circle'을 배치합니다. 보통 '0'에 남겨두고 'centerX'및 'centerY'좌표를 설정합니다. 'layoutX'와'layoutY'는 노드가있는 사각형 인 "layout bounds"왼쪽 위의 좌표입니다 - 그래서 이들은 원을 포함하는 가장 작은 사각형의 왼쪽 위 모서리의 좌표입니다. 원에 클릭하여 등록하려면 기본적으로 원의 실제 렌더링 된 픽셀을 클릭해야하며 포함하는 사각형의 왼쪽 위 모서리는 해당 영역의 일부가 아닙니다. –

+0

따라서 if 문은 절대로 true가 아닙니다. 어쨌든 부동 소수점 값을 비교하는 것은 일반적으로 실수입니다. 왜 내 솔루션이 작동하는지 분명합니다. –

관련 문제