Reputation: 81
scene.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent me){
Circle circle = new Circle(10, 20,50);
circle.setFill(Color.GREEN);
root.getChildren().add(circle);
animation = ParallelTransitionBuilder.create()
.node(circle)
.children(
TranslateTransitionBuilder.create()
.duration(Duration.seconds(2))
.fromX(me.getSceneX())
.toX(me.getSceneX()+10)
.build()
)
.cycleCount(Timeline.INDEFINITE)
//.autoReverse(true)
.build();
}
public void play() {
animation.play();
}
//public void stop() {
//animation.stop()
I am trying to let the circle move
when I mouse click
on the scene. Its not happening !
I am not sure if the sequence is wrong i.e. adding objects to the root before animation starts ??
Upvotes: 0
Views: 577
Reputation: 36722
I made a few edits to your code and it seems to work for me
scene.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent me){
Circle circle = new Circle(10, 20,50);
circle.setFill(Color.GREEN);
root.getChildren().add(circle);
Animation animation = ParallelTransitionBuilder.create()
.node(circle)
.children(
TranslateTransitionBuilder.create()
.duration(Duration.seconds(2))
.fromX(me.getSceneX())
.toX(me.getSceneX()+10)
.build()
)
.cycleCount(Timeline.INDEFINITE)
.autoReverse(true)
.build();
animation.play();
}
});
Though, what I still don't get is the purpose of your
public void play() {
animation.play();
}
and where are you calling it !
Upvotes: 1