简体   繁体   中英

How do I get an objects location in the middle of its transition?

I am trying to test and see if my bullet hits the enemy and if it does ill remove it from the pane and from the bullet list. However, I do not know how to look for the position of the bullet while it is in the middle of its animation. how do I do this? thanks!

 case S:
                Circle bullet = new Circle(5);
                bullet.setLayoutX(guns.getGun1().getLayoutX() + guns.getGun1().getWidth());
                bullet.setLayoutY(guns.getGun1().getLayoutY());
                bullets.add(bullet);
                stage1Pane.getChildren().add(bullet);
                TranslateTransition bulletMovement = new TranslateTransition(Duration.seconds(3),bullet);
                bulletMovement.fromXProperty().bind(guns.getGun1().translateXProperty());
                bulletMovement.fromYProperty().bind(guns.getGun1().translateYProperty());
                bulletMovement.toXProperty().bind(guns.getGun1().translateXProperty().add(1000));
                bulletMovement.toYProperty().bind(guns.getGun1().translateYProperty());
                bulletMovement.play();

                    if (bullet.getCenterX() == enemy.getEnemyBody().getLayoutX())
                    {
                        bulletMovement.stop();
                        enemy.getEnemyLegs().setLayoutX(300);
                    }

                bulletMovement.setOnFinished(i->{
                    System.out.println(bullet.getCenterX() + "   " + enemy.getEnemyBody().getLayoutX());
                    bullets.remove(bullet);
                    stage1Pane.getChildren().remove(bullet);
                });
                break;

You can just add a listener to its boundsInParent property. The listener will be invoked every time the bounds change, ie every time it moves:

bullet.boundsInParentProperty().addListener((obs, oldBounds, newBounds) -> {
    double centerX = newBounds.getMinX() + newBounds.getWidth() /2 ;
    // ... etc
});

Note that the TranslateTransition works by changing the translateX and translateY properties of the node: so the circle's centerX and centerY properties will not change during the animation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM