JavaFX:启动和暂停按钮无法正常运行

时间:2015-05-04 18:19:49

标签: animation javafx

所以我正在尝试创建一个正常运行的“开始”和“暂停”按钮。到目前为止,我有以下代码:

    import javafx.animation.KeyFrame;
    import javafx.animation.Timeline;
    import javafx.application.Application;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Circle;
    import javafx.stage.Stage;
    import javafx.util.Duration;

    public class pauseHelp extends Application{

private int xSpeed = 2;

public static void main(String[] args){
    Application.launch(args);
}
public void start(Stage first){
    Group root = new Group();
    Scene field = new Scene(root, 500, 500);
    field.setFill(Color.GREY);

    Circle ball = new Circle(20);
    ball.setFill(Color.RED);
    ball.setCenterX(field.getHeight()/2);
    ball.setCenterY(field.getWidth()/2);

    Button btnStart=new Button("Start"), btnPause = new Button("Pause");
    btnPause.setLayoutX(50);

    root.getChildren().addAll(ball,btnStart,btnPause);

    first.setScene(field);
    first.show();
    pauseGame(btnPause,ball);
    startGame(btnStart,ball);
}

private void begin(Circle ball, boolean active){
    KeyFrame k = new KeyFrame(Duration.millis(10), e ->{
        moveBall(ball,active);
    });
    Timeline t = new Timeline(k);
    t.setCycleCount(Timeline.INDEFINITE);
    t.play();
}

private void moveBall(Circle ball, boolean active){
    if(active==true){
    ball.setCenterX(ball.getCenterX()+xSpeed);
    if(ball.getCenterX()>=500||ball.getCenterX()<=0){
        xSpeed=-xSpeed;
    }}
}

private void startGame(Button start, Circle ball){
    start.setOnAction(e->{
        begin(ball,true);
    });
}

private void pauseGame(Button pause, Circle ball){
    pause.setOnAction(e->{
        begin(ball,false);
    });
}}

我经常遇到的问题是我无法激活暂停按钮,这意味着当我点击它时没有任何反应。 我对这段代码的另一个问题是每次点击开始按钮时球加速(我发现它与KeyFrame持续时间有关,但无法弄清楚如何更改它)。

我尝试使用时间轴功能,例如pause(),但它们也没有改变任何内容。

1 个答案:

答案 0 :(得分:0)

调用template <class OldAllocator> struct modernize_allocator: OldAllocator { using base=OldAllocator; using T=typename base::value_type; using base::base; template <typename U> struct rebind { using other=modernize_allocator<typename base::rebind<U>::other>; }; using base::construct; template<class...Args> void construct(const typename base::pointer ptr, Args&&...args) { new (ptr) T(std::forward<Args>(args)...); } }; 时,无需创建多个TimeLine。而只创建一个时间轴,使用按钮start()start分别播放和暂停。

将TimeLine定义为实例变量,以便可以在任何地方访问它。在pause

中初始化它
start()

您可能还想完全删除方法import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.stage.Stage; import javafx.util.Duration; public class PauseHelp extends Application{ private Timeline t; private int xSpeed = 2; public static void main(String[] args){ Application.launch(args); } public void start(Stage first){ Group root = new Group(); Scene field = new Scene(root, 500, 500); field.setFill(Color.GREY); Circle ball = new Circle(20); ball.setFill(Color.RED); ball.setCenterX(field.getHeight()/2); ball.setCenterY(field.getWidth()/2); Button btnStart=new Button("Start"), btnPause = new Button("Pause"); btnPause.setLayoutX(50); root.getChildren().addAll(ball,btnStart,btnPause); first.setScene(field); first.show(); pauseGame(btnPause,ball); startGame(btnStart,ball); KeyFrame k = new KeyFrame(Duration.millis(10), e ->{ moveBall(ball); }); t = new Timeline(k); t.setCycleCount(Timeline.INDEFINITE); } private void moveBall(Circle ball){ ball.setCenterX(ball.getCenterX()+xSpeed); if(ball.getCenterX()>=500||ball.getCenterX()<=0){ xSpeed=-xSpeed; } } private void startGame(Button start, Circle ball){ start.setOnAction(e->{ t.play(); }); } private void pauseGame(Button pause, Circle ball){ pause.setOnAction(e->{ t.pause(); }); } } startGame(),并将这些行放在pauseGame()

start()