0

I am currently trying to create a dodge-the-asteroid game in which the user controls a spaceship and tries to avoid the asteroids (3 kinds, small medium large). I currently have the animations all working (spaceship moving, asteroids falling). When the user presses start, the animation for randomly inserting asteroids begins:

public void startGame()
{
    Timeline asteroids = new Timeline(new KeyFrame(Duration.seconds(.5), e -> displayAsteroid()));
    asteroids.setCycleCount(Timeline.INDEFINITE);
    asteroids.play();
}

Here is the code for creating the asteroids everytime the Timeline runs:

public void displayAsteroid()
{
    // creates an asteroid object (constructor randomly creates one of 3 sizes)
    Asteroid asteroid = new Asteroid();
    getChildren().add(asteroid); 

    // randomly generates x coordinate from the board size
    Random rand = new Random();
    int randomNum = rand.nextInt((int) (getWidth() - asteroid.getWidth()));
    //sets the x and y (y is -200 so that the asteroid doesn't just appear at top
    // x is set to the random number
    asteroid.setY(-200);
    asteroid.setX(randomNum);
    asteroid.setTranslateY(asteroid.getY());
    asteroid.setTranslateX(randomNum);

    // animation to move the asteroid down the screen
    Timeline timeline = new Timeline();
    KeyFrame keyFrame = new KeyFrame(Duration.millis(50), event -> {
        // checks if the asteroid leaves the screen or if collision happens
        if(asteroid.getY() > getHeight() || spaceShip.intersects(asteroid.getBoundsInParent()))
        {
            timeline.stop();
            getChildren().remove(asteroid);
        }
        else
        {
            // getSpeed is the speed of the asteroid (small is fastest, large is slowest)
            asteroid.setY(asteroid.getY()+asteroid.getSpeed());
            asteroid.setTranslateY(asteroid.getY());
        }
    });
    timeline.setCycleCount(Animation.INDEFINITE);
    timeline.getKeyFrames().add(keyFrame);
    timeline.play();
}

The spaceship is a pane (bunch of rectangles/ellipses) stacked onto eachother, and the asteroids are also panes with random ellipses stacked ontop of a big ellipse (to make it look like an asteroid).

As of now, the collision does not work, as it will randomly delete one every now and then and I had a bunch disappear as I hit one of them - several times. How can I change this code up so it detects when the asteroid comes into contact with the ship?

Yes, I have looked at other questions, but they were normal objects such as circles, which is why I haven't been able to get mine to work. I can provide additional code (asteroid/spaceship classes) to help you test this out if needed

I feel the logic is wrong behind my code, and any help is greatly appreciated! Thank you, have a nice day ! :)

  • 1
    See also [*Constantly checking if a bullet has touched a node*](https://stackoverflow.com/q/39185306/230513). – trashgod Mar 03 '19 at 20:04
  • @trashgod unfortunately, that didn't work because it uses "Shape", and the asteroid and spaceship are not shapes – Oscar Lopez Mar 03 '19 at 20:14
  • Check whichever `intersects()` you're using in an `Interpolator` _during_ the animation, as shown in the example. – trashgod Mar 03 '19 at 20:46

0 Answers0