I read recently a lesson about JAVAFX, and I want to make an application to assimilate as well what I learnt.
I would like to do a 2D game, where a ball is animated throught a parabolic way.
I'm using for that two objects, Timeline
and Interpolator
.
I must admit that i don't know how exactly the Interpolator
works, my problem is that I want to make the animation stop when the ball arrives at the bottom of the window.
I thought about using 2 methods:
- The first one consist of stopping the animation when the Y coordinate of the ball (
Circle
) is greater or equal to the windows's height, but I don't know how to do that. - The second one consist of stoping the animation at a special moment
Tf
, I got a formula to know when the ball intersects the goal, but I don't know how to define an end-time for the animation.
I would like to have a response for the both methods if possible :)
Here's my Timeline
code:
Timeline t1 = new Timeline();
t1.setAutoReverse(false);
t1.setCycleCount(Timeline.INDEFINITE);
t1.getKeyFrames().addAll(
new KeyFrame(new Duration(1000 * Tf),new KeyValue(this.centerXProperty(),0,new Interpolator(){
protected double curve(double t){
double resultat = coeffAngleX * t;
return - resultat;
}
})),
new KeyFrame(new Duration(1000 * Tf),new KeyValue(this.centerYProperty(),0,new Interpolator(){
protected double curve(double t) {
double resultat = (a * t * t)
+ (coeffAngleY * t);
return resultat;
}
}))
);
Edit:
Ok, I read everything at the link that you gave me, and I understand now that the second parameter of the KeyValue
is the endValue
!
Now look at this example, I set the end value of (X,Y) to (500,700), but look what I got as a result in the console while printing the values of centerXProperty and centerYProperty:
Here's a the new code of mine:
Timeline t1 = new Timeline();
t1.setAutoReverse(false);
t1.setCycleCount(1);
KeyValue xKvB = new KeyValue(this.centerXProperty(),xCenter);
KeyValue yKvB = new KeyValue(this.centerYProperty(),yCenter);
KeyValue xKvE = new KeyValue(this.centerXProperty(),500.0,new Interpolator(){
protected double curve(double t){
double resultat = coeffAngleX * t;
System.out.println("X = " + centerXProperty().get());
return resultat;
}
});
KeyValue yKvE = new KeyValue(this.centerYProperty(),700.0,new Interpolator(){
protected double curve(double t){
double resultat = (a * t * t) + (coeffAngleY * t);
System.out.println("Y = " + centerYProperty().get());
return resultat;
}
});
KeyFrame KfB = new KeyFrame(Duration.ZERO,xKvB,yKvB);
KeyFrame KfE = new KeyFrame(Duration.seconds(1),xKvE,yKvE);
t1.getKeyFrames().addAll(KfB,KfE);
t1.play();