0

I need to create an animation where there is a ball in the screen. I need to stop the ball one it gets the bound of the screen.

I've tried to use this solution:

boolean collision = 
bullet.getBoundsInParent().getMaxX()>=View.getInstance().getXLimit() || 
bullet.getBoundsInLocal().getMaxY()>=View.getInstance().getYLimit();

In order to detect the collision, but it works only for a time! I've tried to relocate the ball by using the "relocate()" method, but when i class "getBoundsInParent().getMaxX()" it return the same value of the screen limit, and i can't restart the animation.

How can I solve the problem?

1 Answers1

0

Since you did not provide any relevant code, I will use the example from here.

Replace

deltaX *= -1; and deltaY *= -1;

with

deltaX = 0;
deltaY = 0;

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);
    }
}
SedJ601
  • 12,173
  • 3
  • 41
  • 59