如何创建JavaFX的转型同样定时的元素呢? [英] How to create JavaFX Transition with Equally-Timed Elements?

查看:191
本文介绍了如何创建JavaFX的转型同样定时的元素呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与JavaFX和动画,尤其是 PathTransition 实验。我创建一个简单的程序,使一个球反弹,而无需使用 QuadCurveTo 类。这里是我的code迄今:

I'm experimenting with JavaFX and animations, especially PathTransition. I'm creating a simple program that makes a ball "bounce," without using the QuadCurveTo class. Here is my code so far:

Ellipse ball = new Ellipse(375, 250, 10, 10);
root.getChildren().add(ball);

Path path = new Path();
path.getElements().add(new MoveTo(375, 500));

int posX = 375;
int posY = 500;
int changeX = 10;
int changeY = 50;
int gravity = 10; // approximate in m/s^2
int sec = 0;

for(; posY<=500; sec++, posX-=changeX, posY-=changeY, changeY-=gravity)
    path.getElements().add(new LineTo(posX, posY));
    // How do I equally space these elements?

PathTransition pathTransition = new PathTransition();
pathTransition.setDuration(Duration.millis(sec*1000));
pathTransition.setNode(ball);
pathTransition.setAutoReverse(true);
pathTransition.setCycleCount(Timeline.INDEFINITE);
pathTransition.setInterpolator(Interpolator.LINEAR);
pathTransition.setPath(path);
pathTransition.play();

我要通过二次序列运行循环,并在正确的运动球移动(曲线路径)。

I have the for loop running through a quadratic sequence, and the ball moves in the correct motion (a curved path).

不过,我希望它在曲线(顶点)的顶部移动慢,因为它是移动的距离越短(如 changeY ,垂直增量变量,正在减少作为循环的推移)来模拟更真实的曲线。然而,它的线速度行进在整个全职工作。

However, I want it to move slower at the top of the curve (vertex) because it is moving less distance (as changeY, the vertical increment variable, is decreasing as the loop goes on) to simulate a more realistic curve. However, it is traveling in a linear speed throughout the full time.

有没有什么办法让每个等距(境)时间的元素,让这个反弹会正确显示?谢谢你。

Is there any way to make each of the elements equally spaced (throughout) time, so that this "bounce" would show correctly? Thanks.

推荐答案

我不会用一个时间表或过渡在所有的这一点。使用AnimationTimer并计算基于自上一帧的经过时间的新坐标。在 AnimationTimer 具有被每个渲染帧调用一次处理方法,采取这一重新presents一个值时间戳纳秒。

I wouldn't use a timeline or transition at all for this. Use an AnimationTimer and compute the new coordinates based on the elapsed time since the last frame. The AnimationTimer has a handle method which is invoked once per rendering frame, taking a value that represents a timestamp in nanoseconds.

SSCCE(与加入到物理弹性):

SSCCE (with elasticity added to the physics):

import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class BouncingBall extends Application {

    @Override
    public void start(Stage primaryStage) {
        Circle ball = new Circle(20, 80, 10);
        ball.setFill(Color.DARKBLUE);
        Pane pane = new Pane(ball);

        AnimationTimer timer = new AnimationTimer() {

            long lastUpdate = 0 ;
            double changeX = 0.1 ;
            double changeY = 0 ;
            double gravity = 10 ;
            double elasticity = 0.95 ;

            @Override
            public void handle(long now) {
                if (lastUpdate == 0) {
                    lastUpdate = now ;
                    return ;
                }
                long elapsedNanos = now - lastUpdate;
                double elapsedSeconds = elapsedNanos / 1_000_000_000.0 ;
                lastUpdate = now ;
                ball.setCenterX(ball.getCenterX() + changeX);
                if (ball.getCenterY() + changeY + ball.getRadius() >= pane.getHeight()) {
                    changeY = - changeY * elasticity;
                } else {
                    changeY = changeY + gravity * elapsedSeconds ;
                }
                ball.setCenterY(Math.min(ball.getCenterY() + changeY, pane.getHeight() - ball.getRadius()));
            }

        };

        primaryStage.setScene(new Scene(pane, 400, 400));
        primaryStage.show();
        timer.start();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

这篇关于如何创建JavaFX的转型同样定时的元素呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆