-1

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.

Sai Dandem
  • 8,229
  • 11
  • 26
Onios
  • 3
  • 2
  • 3
    You will need to supply a [mcve] if you want to get debugging help. Please study the linked page carefully, you should not supply your whole app, but should supply something that replicates just the problem and compiles and runs with just copy and paste. All necessary resources for replication need to be supplied, either the images or replace images which shapes like circles. – jewelsea Jan 31 '22 at 20:03
  • 1
    See also [_Constantly checking if a bullet has touched a node_](https://stackoverflow.com/q/39185306/230513). – trashgod Jan 31 '22 at 20:48

1 Answers1

2

As @jewelsea suggested, you need to supply a minimal reproducible example to get some proper help.

Having said that, based on the code you provided, below is my initial analysis. Note that this is a rough analysis by reading the code.. so everything is just an assumption ;)

The first part of the code (do-while) is to determine the distance the bullet needs to travel (determining the value of register)

The second part is to initiate animation of bullet to translate from its current position (using the register value).

The third part is to set the final translate value to the bullet and the final part is to check if the bounds are intersected and delete the node.

I think the thrid part (below line of code) is not needed and could be the cause for your issue. You are updating the value to the end value and immediately checking if it is intersected.. which will be true and will delete instantly.

bulletObj.getImageBullet().setTranslateX(bulletObj.getImageBullet().getTranslateX() + register - 18);

Try moving your 'for' loop code to onFinished of translation. Something like..

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.setOnFinished(e->{
    for (GameObject platform: PlayField.platforms) { 
        if (platform.getImage().getBoundsInParent().intersects(bulletObj.getImageBullet().getBoundsInParent()))
        {
            tankRoot.getChildren().remove(platform.getImage());
            PlayField.platforms.remove(platform); 
            LevelData.LevelOne[x][y] = "0";
            break;
        }
    }
});
bulletTranslate.play();
Sai Dandem
  • 8,229
  • 11
  • 26