Mssm
Mssm

Reputation: 787

Modify time of animation JavaFX (Timeline + Interpolator)

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:

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:

Result

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();

Upvotes: 1

Views: 819

Answers (2)

trashgod
trashgod

Reputation: 205795

I want to make the animation stop when the ball arrives at the bottom of the window.

Starting from this complete example, Simply set the cycle count to 1, the default.

timeline.setCycleCount(1);

In flight:

image during

Landed:

image end

An algebraically equivalent parabolic Interpolator is seen here.

Upvotes: 1

mipa
mipa

Reputation: 10650

For things like this I find using an AnimationTimer more flexible than using a Timeline. With an AnimationTimer you can directly implement both of your two basic approaches. You either just call the stop method of the timer or inside your timer method you just stop advancing the ball when you have reached the end condition.

Upvotes: 1

Related Questions