Animation 在JavaFX中,当一个元素到达屏幕边界时,我如何停止它?

Animation 在JavaFX中,当一个元素到达屏幕边界时,我如何停止它?,animation,javafx,Animation,Javafx,我需要创建一个动画,其中有一个球在屏幕上。我需要阻止球,一旦球越过屏幕 我已尝试使用此解决方案: boolean collision = bullet.getBoundsInParent().getMaxX()>=View.getInstance().getXLimit() || bullet.getBoundsInLocal().getMaxY()>=View.getInstance().getYLimit(); 为了检测碰撞,但它只能工作一段时间!我尝试使用“relocat

我需要创建一个动画,其中有一个球在屏幕上。我需要阻止球,一旦球越过屏幕

我已尝试使用此解决方案:

boolean collision = 
bullet.getBoundsInParent().getMaxX()>=View.getInstance().getXLimit() || 
bullet.getBoundsInLocal().getMaxY()>=View.getInstance().getYLimit();
为了检测碰撞,但它只能工作一段时间!我尝试使用“relocate()”方法重新定位球,但当我将“getBoundsInParent().getMaxX()”类化时,它返回与屏幕限制相同的值,并且我无法重新启动动画


如何解决此问题?

由于您没有提供任何相关代码,我将使用中的示例

替换

deltaX*=-1;和deltaY*=-1


deltaX=0
deltaY=0

导入javafx.animation.KeyFrame;
导入javafx.animation.Timeline;
导入javafx.application.application;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.geometry.Bounds;
导入javafx.scene.scene;
导入javafx.scene.layout.Pane;
导入javafx.scene.paint.Color;
导入javafx.scene.shape.Circle;
导入javafx.stage.stage;
导入javafx.util.Duration;
公共类游戏实践扩展了应用程序
{
公共静态圈;
公共静态窗格画布;
@凌驾
公共作废开始(最终阶段初级阶段)
{
canvas=新窗格();
最终场景=新场景(画布,800600);
初级阶段。片名(“游戏”);
初级阶段。场景(场景);
primaryStage.show();
圆圈=新圆圈(15,颜色为蓝色);
圈。重新定位(100100);
canvas.getChildren().addAll(圆形);
最终时间线循环=新时间线(新关键帧(Duration.millis(10),新EventHandler()
{
双deltaX=3;
双三角洲=3;
@凌驾
公共无效句柄(最终操作事件t)
{
circle.setLayoutX(circle.getLayoutX()+deltaX);
circle.setLayoutY(circle.getLayoutY()+deltaY);
final Bounds=canvas.getBoundsInLocal();
最终布尔值atRightBorder=circle.getLayoutX()>=(bounds.getMaxX()-circle.getRadius());
最终布尔值atLeftBorder=circle.getLayoutX()=(bounds.getMaxY()-circle.getRadius());
最后一个布尔值attoporder=circle.getLayoutY()请阅读并采取相应行动。
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;

public class GamePractice extends Application
{

    public static Circle circle;
    public static Pane canvas;

    @Override
    public void start(final Stage primaryStage)
    {

        canvas = new Pane();
        final Scene scene = new Scene(canvas, 800, 600);

        primaryStage.setTitle("Game");
        primaryStage.setScene(scene);
        primaryStage.show();

        circle = new Circle(15, Color.BLUE);
        circle.relocate(100, 100);

        canvas.getChildren().addAll(circle);

        final Timeline loop = new Timeline(new KeyFrame(Duration.millis(10), new EventHandler<ActionEvent>()
        {

            double deltaX = 3;
            double deltaY = 3;

            @Override
            public void handle(final ActionEvent t)
            {
                circle.setLayoutX(circle.getLayoutX() + deltaX);
                circle.setLayoutY(circle.getLayoutY() + deltaY);

                final Bounds bounds = canvas.getBoundsInLocal();
                final boolean atRightBorder = circle.getLayoutX() >= (bounds.getMaxX() - circle.getRadius());
                final boolean atLeftBorder = circle.getLayoutX() <= (bounds.getMinX() + circle.getRadius());
                final boolean atBottomBorder = circle.getLayoutY() >= (bounds.getMaxY() - circle.getRadius());
                final boolean atTopBorder = circle.getLayoutY() <= (bounds.getMinY() + circle.getRadius());

                if (atRightBorder || atLeftBorder) {
                    deltaX = 0;
                    deltaY = 0;
                }
                if (atBottomBorder || atTopBorder) {
                    deltaY = 0;
                    deltaX = 0;
                }
            }
        }));

        loop.setCycleCount(Timeline.INDEFINITE);
        loop.play();
    }

    public static void main(final String[] args)
    {
        launch(args);
    }
}