JavaFX 检测与舞台的碰撞
JavaFX detecting collision with the Stage
我创建了一个程序,可以在屏幕上创建一个小黑圈,可以使用 W A S D 键盘键移动。现在我正在努力使黑色圆圈不会超出舞台范围(目前确实如此)。我的想法是创建一个采用 2 个参数的方法:circle 和 stage。该方法将像这样工作:
if(circle.getBoundsInParent().intersects(stage)) {
MOVEMENT_SPEED = 0
}
这个方法应该是检查圆是否与舞台相交,如果是,那么它会将球的移动速度设置为零,从而阻止他通过舞台。然而,
circle.getBoundsInParent().intersects(stage))
代码无效。它说舞台不能转换为边界。我需要做什么来检查图形和舞台碰撞并防止图形移出舞台键?
这是我当前的代码。
package pong;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class Main extends Application {
private static int MOVEMENT_SPEED = 10;
@Override
public void start(Stage primaryStage) {
final Circle circle = createCircle();
final Group group = new Group( circle);
Scene scene = new Scene(group, 700, 700);
move(scene, circle);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
private void move(Scene scene, final Circle circle) {
scene.setOnKeyPressed((KeyEvent event) -> {
switch (event.getCode()) {
case W: circle.setCenterY(circle.getCenterY() - MOVEMENT_SPEED);
break;
case D: circle.setCenterX(circle.getCenterX() + MOVEMENT_SPEED);
break;
case S: circle.setCenterY(circle.getCenterY() + MOVEMENT_SPEED);
break;
case A: circle.setCenterX(circle.getCenterX() - MOVEMENT_SPEED);
break;
}
});
}
private Circle createCircle() {
final Circle circle = new Circle(10, 10, 10, Color.BLACK);
circle.setOpacity(0.7);
return circle;
}
// This method should detect collision and prevent it from happening
private void detectCollsion(Circle circle, Stage primaryStage) {
/* if(circle.getBoundsInParent().intersects(primaryStage)) {
MOVEMENT_SPEED = 0;
} */
}
}
有多种方法可以做到这一点。我觉得这会适合你的情况。
您正在使用圆的 centerX
和 centerY
属性来移动圆。这些属性 return 圆心的位置,我们可以将其用于碰撞检测。与场景边缘相交的圆周上的确切点的位置将是该值与圆的半径之和。
我们总能得到场景的引用(node.getScene()
),因此找到允许圆移动的边界。在你的情况下,我们已经有了对场景的引用,所以我们只使用它。
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class Main extends Application{
private static int MOVEMENT_SPEED = 10;
private static int RADIUS = 10;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
final Circle circle = createCircle();
final Group group = new Group( circle);
Scene scene = new Scene(group, 700, 700);
move(scene, circle);
primaryStage.setScene(scene);
primaryStage.show();
}
private void move(Scene scene, final Circle circle) {
scene.setOnKeyPressed((KeyEvent event) -> {
switch (event.getCode()) {
case W:
if(circle.getCenterY() > RADIUS)
circle.setCenterY(circle.getCenterY() - MOVEMENT_SPEED);
break;
case D:
if(circle.getCenterX() < scene.getWidth() - RADIUS)
circle.setCenterX(circle.getCenterX() + MOVEMENT_SPEED);
break;
case S:
if(circle.getCenterY() < scene.getHeight() - RADIUS)
circle.setCenterY(circle.getCenterY() + MOVEMENT_SPEED);
break;
case A:
if(circle.getCenterX() > RADIUS)
circle.setCenterX(circle.getCenterX() - MOVEMENT_SPEED);
break;
}
});
}
private Circle createCircle() {
final Circle circle = new Circle(10, 10, RADIUS, Color.BLACK);
circle.setOpacity(0.7);
return circle;
}
}
我创建了一个程序,可以在屏幕上创建一个小黑圈,可以使用 W A S D 键盘键移动。现在我正在努力使黑色圆圈不会超出舞台范围(目前确实如此)。我的想法是创建一个采用 2 个参数的方法:circle 和 stage。该方法将像这样工作:
if(circle.getBoundsInParent().intersects(stage)) {
MOVEMENT_SPEED = 0
}
这个方法应该是检查圆是否与舞台相交,如果是,那么它会将球的移动速度设置为零,从而阻止他通过舞台。然而,
circle.getBoundsInParent().intersects(stage))
代码无效。它说舞台不能转换为边界。我需要做什么来检查图形和舞台碰撞并防止图形移出舞台键?
这是我当前的代码。
package pong;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class Main extends Application {
private static int MOVEMENT_SPEED = 10;
@Override
public void start(Stage primaryStage) {
final Circle circle = createCircle();
final Group group = new Group( circle);
Scene scene = new Scene(group, 700, 700);
move(scene, circle);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
private void move(Scene scene, final Circle circle) {
scene.setOnKeyPressed((KeyEvent event) -> {
switch (event.getCode()) {
case W: circle.setCenterY(circle.getCenterY() - MOVEMENT_SPEED);
break;
case D: circle.setCenterX(circle.getCenterX() + MOVEMENT_SPEED);
break;
case S: circle.setCenterY(circle.getCenterY() + MOVEMENT_SPEED);
break;
case A: circle.setCenterX(circle.getCenterX() - MOVEMENT_SPEED);
break;
}
});
}
private Circle createCircle() {
final Circle circle = new Circle(10, 10, 10, Color.BLACK);
circle.setOpacity(0.7);
return circle;
}
// This method should detect collision and prevent it from happening
private void detectCollsion(Circle circle, Stage primaryStage) {
/* if(circle.getBoundsInParent().intersects(primaryStage)) {
MOVEMENT_SPEED = 0;
} */
}
}
有多种方法可以做到这一点。我觉得这会适合你的情况。
您正在使用圆的 centerX
和 centerY
属性来移动圆。这些属性 return 圆心的位置,我们可以将其用于碰撞检测。与场景边缘相交的圆周上的确切点的位置将是该值与圆的半径之和。
我们总能得到场景的引用(node.getScene()
),因此找到允许圆移动的边界。在你的情况下,我们已经有了对场景的引用,所以我们只使用它。
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class Main extends Application{
private static int MOVEMENT_SPEED = 10;
private static int RADIUS = 10;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
final Circle circle = createCircle();
final Group group = new Group( circle);
Scene scene = new Scene(group, 700, 700);
move(scene, circle);
primaryStage.setScene(scene);
primaryStage.show();
}
private void move(Scene scene, final Circle circle) {
scene.setOnKeyPressed((KeyEvent event) -> {
switch (event.getCode()) {
case W:
if(circle.getCenterY() > RADIUS)
circle.setCenterY(circle.getCenterY() - MOVEMENT_SPEED);
break;
case D:
if(circle.getCenterX() < scene.getWidth() - RADIUS)
circle.setCenterX(circle.getCenterX() + MOVEMENT_SPEED);
break;
case S:
if(circle.getCenterY() < scene.getHeight() - RADIUS)
circle.setCenterY(circle.getCenterY() + MOVEMENT_SPEED);
break;
case A:
if(circle.getCenterX() > RADIUS)
circle.setCenterX(circle.getCenterX() - MOVEMENT_SPEED);
break;
}
});
}
private Circle createCircle() {
final Circle circle = new Circle(10, 10, RADIUS, Color.BLACK);
circle.setOpacity(0.7);
return circle;
}
}