私はボールが画面の周りでバウンスするプログラムを書いていますが、起動するとボールは全く動きません。私は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);
}
私は増加速度を取り、彼らは無関係に見えた原因(スピードを不思議包みanyonesがanimation.setRateに設定したスピード方法を下げる(animation.getRate ()+ 0.1)。私のボールは、()まったく動いていない理由、それはコーナーにとどまる?
あなたはどのコードを動かすと思いますか? –
これらはどちらも継承(ネットビーンズでは同じパッケージに入っています)で接続されていますので、メソッドの意味に最初のコードを使用しています.2番目のファイルはアプリケーションに拡張され、プログラムを起動するmainメソッドを持っています全体として。だから私は全体のプログラムを起動するために2番目を使用していた。 – NarinderRSharma
だから、最初のプログラムは、タイムラインアニメーション=新しいタイムラインとmoveball()メソッドを持っているので、それを動かすと思う。 – NarinderRSharma