簡體   English   中英

JavaFX檢測與舞台的碰撞

[英]JavaFX detecting collision with the Stage

我創建了一個程序,該程序在屏幕上創建了一個小的黑色圓圈,該圓圈可以使用WASD鍵盤按鍵移動。 現在,我正在嘗試使黑圈不會超出Stage邊界(它目前正在這樣做)。 我的想法是創建一個采用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;
    }   */
 }    
}

有多種方法可以做到這一點。 我認為這將適合您的情況。

您正在使用圓的centerXcenterY屬性來移動圓。 這些屬性返回圓心的位置,我們可以將其用於碰撞檢測。 圓的圓周上與場景邊緣相交的精確點的位置將是該值與圓的半徑之和。

我們總是可以獲取到場景的引用( 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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM