'Obstacles are removed faster than the bullet animation passes

Situation: The tank has a Shot() method and a code that checks if the bullet hits the obstacle after being fired. The bullet flies up the X-coordinate. The do...while loop checks how far the bullet can travel without obstacles. After that, the animation of the bullet flight itself takes place through TranslateTransition. And the last loop goes through all the game obstacles and checks for contact through intersects and, if successful, removes the obstacle.

do {
    y = (int) (bulletObj.getImageBullet().getTranslateX() + register) / PlayField.BRICK_SIZE;
    x = (int) bulletObj.getImageBullet().getTranslateY() / PlayField.BRICK_SIZE;
    line = LevelData.LevelOne[x][y];
    register += 1;
} while (line.equals("0"));

System.out.println(System.currentTimeMillis()); // 1643642047472 ms.

bulletTranslate = new TranslateTransition();
bulletTranslate.setFromX(bulletObj.getImageBullet().getTranslateX());
bulletTranslate.setToX(bulletObj.getImageBullet().getTranslateX() + register - 18);
bulletTranslate.setNode(bulletObj.getImageBullet());
bulletTranslate.setDuration(Duration.millis(register)); // Let register = 300 мs.
bulletTranslate.play();

System.out.println(System.currentTimeMillis()); // 1643642047474 ms.

bulletObj.getImageBullet().setTranslateX(bulletObj.getImageBullet().getTranslateX() + register - 18);
for (GameObject platform: PlayField.platforms) { 
    if (platform.getImage().getBoundsInParent().intersects(bulletObj.getImageBullet().getBoundsInParent()))
    {

        System.out.println(System.currentTimeMillis()); // 1643642047474 ms.

        tankRoot.getChildren().remove(platform.getImage());
        PlayField.platforms.remove(platform); 
        LevelData.LevelOne[x][y] = "0";
        break;
    }
}

Everything works as expected, but there is only one problem.

The problem is that the obstacle objects are removed faster than the animation bullets pass.

And it is necessary that after contact they are deleted simultaneously.

How to solve the problem?

Before the shot

enter image description here

After the shot, the object disappeared during the flight of the bullet

enter image description here

P.S Sorry, I used google translate.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source