【发布时间】:2016-03-29 23:34:55
【问题描述】:
所以我现在有一个程序可以使用 JavaFX 在屏幕上移动一个弹跳球,现在我尝试在我的时间轴动画中重新格式化 Duration.millis() 下的某些值,我把它放得越低,球跑得越快但是,有人告诉我这不是最好的方法,我应该询问动态速度以添加到我的程序中这是我的球运动代码:
public class BallPane extends Pane {
public final double radius = 5;
public double x = radius, y = radius;
public double dx = 1, dy = 1;
public Circle circle = new Circle(x, y, radius);
public Timeline animation;
public BallPane(){
circle.setFill(Color.BLACK); // Set ball color
getChildren().add(circle); // Place ball into Pane
// Create animation for moving the Ball
animation = new Timeline(
new KeyFrame(Duration.millis(10), e -> moveBall() ));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();
}
public void moveBall() {
// Check Boundaries
if (x < radius || x > getWidth() - radius) {
dx *= -1; //change Ball direction
}
if (y < radius || y > getHeight() - radius) {
dy *= -1; //change Ball direction
}
x += dx;
y += dy;
circle.setCenterX(x);
circle.setCenterY(y);
} }
反过来,这将是一场乒乓球比赛,所以我将有 5 个级别,在每个级别中,我希望球移动得更快我可以通过降低 Duration.millis() 来做到这一点,但有人告诉我这不是增加速度的最佳方法,我该如何在不降低时间线动画参数中的 Duration.millis 的情况下执行此操作?我应该添加其他参数或其他速度方法吗?
【问题讨论】:
-
您可以将
dx和dy乘以速度系数。
标签: java animation javafx game-physics velocity