首页 > 解决方案 > 动画运行时使新动画不可能(在动画期间禁用 setOnMouseClicked)

问题描述

我一直在寻找一种方法来做到这一点,但我似乎无法找到 javafx 的解决方案(我只能在 jQuery 和 javascript 中找到它)。我有一个游戏,你可以在 Mouseclick 上旋转图块(ImageViews),我用

setOnMouseClicked(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            update();
        }
    };

更新有这个:

public void update() {
    Animation animation = new RotateTransition(Duration.millis(100), this);
    animation.setByAngle(90);
    animation.play();
    tile.rotate();
}

问题是,当我在瓷砖上单击得太快(当它仍在旋转时)时,它会被搞砸并卡在例如 60 度。我只是想让它在动画运行时无法单击 imageView。请帮我。提前致谢!

标签: javajavafx

解决方案


您可以添加一个布尔值来存储该功能当前是否正在运行并相应地禁用/启用它。

boolean currentlyPlaying = false;
node.setOnMouseClicked(event -> {
    if (!currentlyPlaying)
        update();
});
public void update() {
    Animation animation = new RotateTransition(Duration.millis(100), this);
    animation.setByAngle(90);
    animation.play();
    currentlyPlaying = true;
    animation.setOnFinished(event -> currentlyPlaying = false;);
    tile.rotate();
}

推荐阅读