【问题标题】:How do I make my bouncing ball move?如何让我的弹跳球移动?
【发布时间】:2016-03-29 20:47:15
【问题描述】:

所以我正在编写一个程序,球在屏幕上弹跳,但是当我启动它时,球根本不动。我使用了 Timeline 的动画值,并使用 dy 和 dx 作为屏幕半径的边界。

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(){
//sets ball position
x += dx; 
y += dy; 
circle.setCenterX(x); 
circle.setCenterY(y);

circle.setFill(Color.BLACK);
getChildren().add(circle); 

// Create animation for moving the Ball
animation = new Timeline(
    new KeyFrame(Duration.millis(50), e -> moveBall() ));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();
}    

public void play(){
    animation.play();
}

public void pause(){
    animation.pause();
}

public DoubleProperty rateProperty(){
    return animation.rateProperty();
}

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
    }
}
}

这是我的启动代码:

    public class BounceBallControl extends Application {

@Override
public void start(Stage stage) {

    BallPane ballPane = new BallPane(); // creates the ball pane

    ballPane.setOnMousePressed( e -> ballPane.pause());
    ballPane.setOnMouseReleased( e -> ballPane.play());

    Scene scene = new Scene(ballPane, 300, 250);
    stage.setTitle("BounceBall!");
    stage.setScene(scene);
    stage.show();

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

我取出了增加速度和减少速度的方法,因为它们似乎无关紧要(以防有人想知道速度设置为 animation.setRate(animation.getRate() + 0.1)。为什么我的球(根本)不动,它留在角落里?

【问题讨论】:

  • 你认为哪个代码会让它动起来?
  • 好吧,它们都是通过继承连接的(在 netbeans 中它们在同一个包中),所以我使用第一个代码进行方法暗示,第二个文件是扩展到 Application 并具有整体启动程序的主要方法。所以我使用第二个来启动整个程序。
  • 所以我猜第一个程序会让它移动,因为它有 Timeline animation = new Timeline 和 moveball() 方法。
  • 嗯?你想让球在动画中移动,对吧?没有与动画相关的代码会导致球改变位置。
  • James_D 有一个good link on bouncing balls here Narinder。

标签: java ios animation javafx timeline


【解决方案1】:

当你移动它时,你实际上并不是relocating 球。

请参阅下面的示例,它将球重新定位在新的 x 和 y 坐标位置以移动它。

public void moveBall() {
    x += dx;
    y += dy;

    // Check Boundaries
    if (x < radius || x > getWidth() - radius) {
        dx *= -1; //change Ball direction
    }
    if (y < radius || y > getHeight() - radius) {
        dy *= -1; //change Ball direction
    }

    circle.setCenterX(x);
    circle.setCenterY(y);
}

注意,您可以完全取消单独的 x 和 y 变量,因为它们的值已经通过圆的 centerX 和 centerY 属性表示,但是我在上面的代码中留下了您的 x 和 y 变量,以便与您最初编码的解决方案没有太大区别。

【讨论】:

  • 哦,看看我所做的是我已经重新定位了程序顶部的 circle.setCenterY() 和 X() 不认为它会有所作为,我没有意识到它在 MoveBall() 方法中必须确保我检查我的“{}”正确。所以如果我想让球速更快,我会提高我的 Duration.millis() 对吗?实际上没关系,我刚刚意识到我会使用我的 increaseSpeed 方法来做到这一点。谢谢
  • 不,改变时间线的持续时间对你来说不是一个好的解决方案(你会得到断断续续的动画或缺乏对球速度的细粒度动态控制,除非你同时使用插值键值)。也许你应该问一个关于改变球移动速度的最佳方法的新问题。
  • 哦,我确实在一两周前从我的书中做了一个例子,它使用一个球绕着一个圆圈(一个椭圆)旋转,它会在曲线上减速,所以我使用了 setInterpolator(Interpolator .线性);我猜这就是插值键的意思。但速度会更有利,对吗?
  • Narinder,请把问题当作问题来提问,这就是 StackOverflow 的工作原理——它会给你最好的答案。
  • 哦,我看到你已经提出了一个新问题 :-) why main method in Java need to be public always?
猜你喜欢
  • 1970-01-01
  • 2013-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-23
  • 1970-01-01
相关资源
最近更新 更多