每次单击按钮时如何才能使时间轴更快? [英] How can I make my timeline faster everytime I click a button?

查看:108
本文介绍了每次单击按钮时如何才能使时间轴更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前在JavaFX中遇到问题,尝试创建一个cookie Clicker游戏,所以我创建了一个Clicker按钮,每隔5秒就会自动点击一次cookie。我试图让我的时间轴每次购买另一个点击器时快0.1秒,目前,我一直在尝试这样:

currently stuck with a problem in JavaFX, trying to create a cookie clicker game, so i've created a button "Clicker" which automatically, once every 5 seconds, clicks the cookie. Im trying to make my timeline 0.1 seconds faster everytime i buy another clicker, currently, i've been trying it like this:

        private double clickerSpeed = 5.1;

        Timeline clickerPoints = new Timeline();
        KeyFrame kfClicker = new KeyFrame(Duration.seconds(clickerSpeed), 
        event -> {
             cookieClicker.cookies += cookieClicker.cursor.getProdRate();
             cookieButton.setText("Cookies = " + cookieClicker.cookies);
        });
        clickerPoints.getKeyFrames().add(kfClicker);
        clickerPoints.setCycleCount(Timeline.INDEFINITE);


        buyClicker.setOnAction(event -> {
            if (clickerSpeed >= 1 && cookieClicker.cookies >= 
            cookieClicker.clicker.getCosts()) {
            clickerSpeed -= 0.1; ## In theory, this should make the keyframe faster?
            }
        cookieClicker.buyClicker();
        clickerLabel.setText(cookieClicker.clicker.getName() + "s: " + 
        cookieClicker.clicker.getLevel());
        buyClicker.setText("Buy clicker for: " + 
        cookieClicker.clicker.getCosts());
        cookieButton.setText("Cookies = " + cookieClicker.cookies);
        clickerPoints.play();


推荐答案

修改原语 double 对之前读取的值没有任何影响;持续时间为不自动调整。

Modifying a primitive double does not have any effect on values read before; the duration is not automatically adjusted.

要加快时间轴,你应该修改费率属性。

To speed up the Timeline you should modify the rate property instead.

以下示例显示如何更新动画所需的持续时间,以便将矩形的淡入淡出从10秒减少到1秒1秒步骤:

The following example shows how to update the duration an animation takes to animate fading a Rectangle from 10 seconds to 1 second in 1 second steps:

@Override
public void start(Stage primaryStage) throws Exception {
    Rectangle rect = new Rectangle(100, 100);
    Button btn = new Button("Speed Up");
    HBox root = new HBox(rect, btn);
    Timeline timeline = new Timeline(
            new KeyFrame(Duration.ZERO, new KeyValue(rect.opacityProperty(), 1d)),
            new KeyFrame(Duration.seconds(10), new KeyValue(rect.opacityProperty(), 0d))
    );
    timeline.setCycleCount(Animation.INDEFINITE);
    timeline.play();

    btn.setOnAction(new EventHandler<ActionEvent>() {

        private double duration = timeline.getCycleDuration().toSeconds();

        @Override
        public void handle(ActionEvent event) {
            if (duration > 1) {
                duration--;
                timeline.setRate(timeline.getCycleDuration().toSeconds() / duration);
            }
        }
    });

    Scene scene = new Scene(root);
    primaryStage.setScene(scene);
    primaryStage.show();
}

这篇关于每次单击按钮时如何才能使时间轴更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆