簡體   English   中英

邊界更新,即使已聲明為最終邊界

[英]Bounds updating even though declared as final

我正在嘗試制作一個程序,它的球在接近屏幕邊界時會(首先)反彈。

但是,當檢查bounds.getmaxY()值時,我可以看到該值正在增加,並且由於我猜想從來沒有使用過if循環。

public class bouncyFX extends Application {


public static void main(String[] args) {
    launch(args);
}
static Pane pane = new Pane();

@Override
public void start(final Stage primaryStage) {
    Scene scene = new Scene(pane, 500, 200);
    primaryStage.setScene(scene);
    primaryStage.show();
    pane.setOnMouseClicked(new EventHandler<MouseEvent>() {
        public void handle(final MouseEvent event) {
            final Ball ball = new Ball(event.getX(), event.getY(), 12, Color.AQUA);
            ball.relocate(event.getX(), event.getY());
            pane.getChildren().addAll(ball);
            final Timeline loop = new Timeline(new KeyFrame(Duration.millis(10), new EventHandler<ActionEvent>() {
                double deltaX = 2;
                double deltaY = 2;
                public void handle(final ActionEvent event) {
                    ball.setLayoutX(ball.getLayoutX() + deltaX);
                    ball.setLayoutY(ball.getLayoutY() + deltaY);
                    ball.Collision(deltaX, deltaY);

                    final Bounds bounds = pane.getBoundsInLocal();
                    final boolean atRightBorder = ball.getLayoutX() >= (bounds.getMaxX()-ball.getRadius());
                    final boolean atLeftBorder = ball.getLayoutX() <= (bounds.getMinX()+ball.getRadius());
                    final boolean atBottomBorder = ball.getLayoutY() >= (bounds.getMaxY()-ball.getRadius());
                    final boolean atTopBorder = ball.getLayoutY() <= (bounds.getMinY()+ball.getRadius());
                    if(atRightBorder || atLeftBorder)
                        deltaX *= -1;
                    if(atBottomBorder ||atTopBorder)
                        deltaY *= -1;
                }
            }));
            loop.setCycleCount(Timeline.INDEFINITE);
            loop.play();
        }
    });
}

您的Bounds變量根本沒有改變,您每次都只獲得一個新實例。

認為這里發生的事情是,您在查詢窗格的邊界之前更改了球的布局。 窗格正在增長以適應球位置的變化。 所以嘗試

                final Bounds bounds = pane.getBoundsInLocal();
                final boolean atRightBorder = ball.getLayoutX() + deltaX >= (bounds.getMaxX()-ball.getRadius());
                final boolean atLeftBorder = ball.getLayoutX() + deltaX <= (bounds.getMinX()+ball.getRadius());
                final boolean atBottomBorder = ball.getLayoutY() + deltaY >= (bounds.getMaxY()-ball.getRadius());
                final boolean atTopBorder = ball.getLayoutY() + deltaY <= (bounds.getMinY()+ball.getRadius());
                if(atRightBorder || atLeftBorder)
                    deltaX *= -1;
                if(atBottomBorder ||atTopBorder)
                    deltaY *= -1;

                ball.setLayoutX(ball.getLayoutX() + deltaX);
                ball.setLayoutY(ball.getLayoutY() + deltaY);

                // not sure what this line does, so you will need to put it where it makes sense:
                ball.Collision(deltaX, deltaY);

將值聲明為final不會使對象不變。 這僅表示對對象的引用將不會更改-即,每次引用bounds ,您將獲得相同的對象。 一旦賦值,最終變量就不能更改為指向另一個Bounds對象。

不能分配最終變量,但是您可以更改它的屬性,因為您不會使其不可變。

看一下有關定義不可變對象的文章

在您的情況下,如果可以訪問Bounds對象,則可以使用此解決方案來實現它,但是如果您不能修改Bounds對象以使其不可變,則需要為Bounds編寫包裝類。

暫無
暫無

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

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