So I'm having a bit of issue getting an animation of mine to behave the way I want. My intent is to have an animation start on a button press (or any given event), play till its end, and then reverse once the button is released (which could happen in the middle of the animation, in which case the node should animate back to it's start position).
I originally tried doing this with just the RotateTransition class (since that's the type of animation I'm doing), but couldn't get the behavior described above, so I started messing around with a Timeline.
This is what I have so far, and works great for releasing before the animation completes, but I can't get the node to animate back to it's original position after it's completed. I can't seem figure out the right combination of setting the CycleCount, AutoReverseProperty, pausing/playing or whether or not I need multiple KeyFrames to express the different parts of the animation.
SVGPath someNode = new SVGPath(); // The node I'm animating
Timeline rotateTimeline= new Timeline();
rotateTimeline.getKeyFrames().add(new KeyFrame(Duration.millis(1000), new KeyValue(someNode.rotateProperty(), -50));
// Events that start/stop the animation -- please ignore the keyCode conditionals as I might have them wrong...just typing them up on the fly as an example
this.setOnKeyPressed(keyEvent -> {
if(keyEvent.getCode() == KeyCode.ENTER) {
rotateTimeline.play();
someNode.getStyleClass().add("pressed"); // Is there a way to toggle style classes instead of having to add/remove them?
}
});
this.setOnKeyReleased(keyEvent -> {
if(keyEvent.getCode() == KeyCode.ENTER) {
rotateTimeline.setRate(-1);
someNode.getStyleClass().remove("pressed");
// This is where I tried all sorts of things, like setting autoreverse to be true, then playing the
// timeline again, having two keyframes and pausing after the first is met and resuming here, and nothing seems to be working for me...
}
});
And not to bundle too many questions into a single thread, but is there any way to edit my forum name? I went to my profile prefs, and the text box to update my forum handle is grayed out... (e.g. http://i.imgur.com/5agcHja.png)