Animation JavaFX:启动和暂停按钮功能不正常

Animation JavaFX:启动和暂停按钮功能不正常,animation,javafx,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.B

所以我试图创建一个功能正常的开始和暂停按钮。到目前为止,我有以下代码:

    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);
    });
}}
导入javafx.animation.KeyFrame;
导入javafx.animation.Timeline;
导入javafx.application.application;
导入javafx.scene.Group;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.paint.Color;
导入javafx.scene.shape.Circle;
导入javafx.stage.stage;
导入javafx.util.Duration;
公共类pauseHelp扩展应用程序{
私有int xSpeed=2;
公共静态void main(字符串[]args){
应用程序启动(args);
}
公开作废开始(第一阶段){
组根=新组();
场景字段=新场景(根,500500);
字段。设置填充(颜色。灰色);
圆球=新的圆(20);
球。塞菲(颜色。红色);
ball.setCenterX(field.getHeight()/2);
ball.setCenterY(field.getWidth()/2);
按钮btnStart=新按钮(“开始”),btnPause=新按钮(“暂停”);
btnPause.setLayoutX(50);
root.getChildren().addAll(ball、btnStart、btnPause);
第一,设置场景(字段);
首先,show();
pauseGame(btnPause,ball);
startGame(btnStart,ball);
}
专用空心起点(圆形球,布尔激活){
关键帧k=新关键帧(持续时间.millis(10),e->{
移动球(球,活动);
});
时间线t=新时间线(k);
t、 setCycleCount(Timeline.unfinite);
t、 play();
}
专用空心移动球(圆形球,布尔激活){
如果(活动==真){
ball.setCenterX(ball.getCenterX()+xSpeed);
如果(ball.getCenterX()>=500 | | ball.getCenterX()){
开始(球,真);
});
}
私人空间暂停(按钮暂停、圆圈球){
暂停。设置动作(e->{
开始(球,假);
});
}}
我经常遇到的问题是,我无法激活暂停按钮,这意味着当我点击它时,什么也不会发生。 我在这段代码中遇到的另一个问题是,每次单击“开始”按钮时,球都会加速(我发现这与关键帧的持续时间有关,但不知道如何更改它)


我尝试使用时间线函数,如
暂停()
,但它们也没有改变任何东西。

调用
开始()
时,不需要创建多个时间线。只创建一个时间线,使用按钮
开始
暂停
分别播放和暂停时间线

将时间线定义为实例变量,以便在任何地方都可以访问它。在
start()
中初始化它


只需创建一个时间线,而不是每次调用
begin(…)
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();
        });
    }
}
btnStart.setOnAction(e-> {
    t.play();
});

btnPause.setOnAction(e->{
    t.pause();
});