【发布时间】:2017-09-13 05:26:26
【问题描述】:
这是我的以下代码:
public void start(Stage primaryStage) throws Exception {
Pane root = new Pane();
Scene scene = new Scene(root, 500, 500, Color.WHITE);
ImageView image = new ImageView(new Image(getClass().getResourceAsStream("dice.jpeg")));
image.setX(35);
image.setY(225);
image.setFitWidth(50);
image.setFitHeight(70);
root.getChildren().add(image);
Line line = new Line(20, 40, 120, 40);
line.setStroke(Color.RED);
line.setStrokeWidth(10);
root.getChildren().add(line);
if (line.getBoundsInParent().intersects(image.getBoundsInParent())) {
System.out.println("intersect");
}
Timeline timeline = new Timeline(
new KeyFrame(
Duration.seconds(2),
new KeyValue(line.translateYProperty(), 600)
));
timeline.setCycleCount(Animation.INDEFINITE);
timeline.setAutoReverse(false);
timeline.play();
primaryStage.setScene(scene);
primaryStage.show();
}
我希望在线条和图像相交时打印此System.out.println("intersect") 消息,但是当我运行我的代码时它不起作用。谁能告诉我我做错了什么?任何帮助表示赞赏!
【问题讨论】:
-
在collision detection 上看到这个问题(也许这是重复的?)。你真的想检查边界交叉点还是你想检查视觉交叉点?当你有动画时,你需要在有东西移动时检查交叉点(例如在AnimationTimer中)而不是在创建时检查。
-
你也可以给
line.translateYProperty()添加一个监听器并检查其中的冲突。 -
是的,在这个只有 y 属性发生变化的简单情况下,它的监听器会很好地工作并且实现起来非常简单。如果您的整个应用程序更复杂,并且每个帧可能会更改多个属性,则动画计时器内的检查可能更易于管理。
标签: java image javafx line intersect