3

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");
        }
    }
  • 3
    I feel like you should have a game loop. – SedJ601 Nov 25 '19 at 00:10
  • 4
    In theory you could add a listener to the `boundsInParent` properties checking for collisions on every move, but you run the risk of causing collisions while updating the scene. E.g. lets say you write a game where obstacles move and the player moves. Let's say you update the player position is updated first. This could cause a collision that is no longer there after moving the obstacle... – fabian Nov 25 '19 at 06:32
  • Like Sedrick said it is always done via a loop which runs permanantly and checks in your case for collisions. [This](https://stackoverflow.com/questions/15013913/checking-collision-of-shapes-with-javafx) may help you – fuggerjaki61 Nov 26 '19 at 16:26

0 Answers0