How can you add a listener to the to the intersects method in JavaFX?

107 Views Asked by At

I'm trying to make a "frogger-like game" for my programming class and I can't seem to figure out how to detect collision. I found the ImageView.intersects method but that only works when I first run the program and it doesn't update when things start moving. I've also looked at BooleanProperty but didn't have any success

public class DisplayPane extends Pane {
    public ImageView vehicle;
    private final Image lamboImage = new Image("File:Lambo.jpg");
    private final Image skiImage = new Image("File:ski.jpg");

    private Rectangle firstObstacle;
    private TranslateTransition firstTransition;


    public DisplayPane() {
vehicle = new ImageView(lamboImage);
        vehicle.setPreserveRatio(true);
        vehicle.setFitWidth(150);
        vehicle.setY(400);
        getChildren().add(vehicle);
        vehicle.setFocusTraversable(true);
        vehicle.setOnKeyTyped(this::ProcessKeyType);

        firstObstacle = new Rectangle(200, 0, 30, 30);
        firstObstacle.setFill(Color.RED);
        firstTransition = new TranslateTransition(Duration.seconds(2), firstObstacle);
        firstTransition.setByY((800));
        firstTransition.setCycleCount(Animation.INDEFINITE);
        firstTransition.play();

        getChildren().add(firstObstacle);
        if (vehicle.intersects(firstObstacle.getBoundsInLocal())) {
            System.out.println("Gsme over");
        }
    }
0

There are 0 best solutions below