Oromis
Oromis

Reputation: 316

Trigger event when reaches half of an PathTransition in JavaFX

I have a PathTransition to move a ImageView composed of 2 paths.

I want to add an action when the animation reaches half of its duration.

I saw that by the getCuePoints method, we could add markers at certain times but I don't know how link this marker at an action :/

Do you have an idea to do that?

Thanks! :)

Upvotes: 0

Views: 84

Answers (1)

fabian
fabian

Reputation: 82461

Use a ParallelTransition to run a PauseTransition with a duration of half of that of the PathTransition. This allows you to use the onFinished handler to trigger the event:

public void start(Stage primaryStage) {
    Path path = new Path(new MoveTo(), new CubicCurveTo(100, 100, 150, 50, 200, 100));
    Rectangle rect = new Rectangle(10, 10);
    Rectangle back = new Rectangle(100, 50, Color.RED);
    back.setVisible(false);

    // original transition
    PathTransition transition = new PathTransition(Duration.seconds(5), path, rect);

    // transition for triggering halftime event
    PauseTransition pause = new PauseTransition(transition.getCycleDuration().multiply(0.5));
    pause.setOnFinished(evt -> back.setVisible(true));

    // combine & play transitions
    ParallelTransition animation = new ParallelTransition(rect, transition, pause);
    animation.play();

    Scene scene = new Scene(new Pane(back, rect), 300, 300);
    primaryStage.setScene(scene);
    primaryStage.show();
}

Upvotes: 3

Related Questions