Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在动画运行时使新动画无法执行(在动画期间禁用setOnMouseClicked)_Java_Javafx - Fatal编程技术网

Java 在动画运行时使新动画无法执行(在动画期间禁用setOnMouseClicked)

Java 在动画运行时使新动画无法执行(在动画期间禁用setOnMouseClicked),java,javafx,Java,Javafx,我一直在寻找实现这一点的方法,但我似乎无法找到javafx的解决方案(我只在jQuery和javascript中找到)。 我有一个游戏,你可以旋转瓷砖(图像视图)鼠标点击,我这样做 setOnMouseClicked(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { update(); } }

我一直在寻找实现这一点的方法,但我似乎无法找到javafx的解决方案(我只在jQuery和javascript中找到)。 我有一个游戏,你可以旋转瓷砖(图像视图)鼠标点击,我这样做

setOnMouseClicked(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            update();
        }
    };
问题是,当我在瓷砖上点击过快时(当它还在旋转时),它会被拧紧并卡在例如60度的位置。 我只是想让它在动画运行时不可能单击imageView。 请帮帮我。
提前谢谢

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

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();
}

另一个选项,而不是创建一个单独的
布尔
变量,是使用
动画
。您还可以对每个磁贴重复使用相同的
动画

import javafx.animation.Animation.Status;
import javafx.animation.RotateTransition;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Main extends Application {

  private RotateTransition transition;

  @Override
  public void start(Stage primaryStage) throws Exception {
    GridPane root = new GridPane();
    root.setPadding(new Insets(20));
    root.setHgap(15);
    root.setVgap(15);

    EventHandler<MouseEvent> onClick = this::handleClick;
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) {
        Rectangle rect = new Rectangle(50, 50);
        rect.setOnMouseClicked(onClick);
        root.add(rect, i, j);
      }
    }

    Scene scene = new Scene(root);
    primaryStage.setScene(scene);
    primaryStage.setTitle("Animation Example");
    primaryStage.setResizable(false);
    primaryStage.show();
  }

  private void handleClick(MouseEvent event) {
    if (transition == null) {
      transition = new RotateTransition(Duration.seconds(100));
      transition.setByAngle(90);
    } else if (transition.getStatus() == Status.RUNNING) {
      return;
    }

    transition.setNode((Rectangle) event.getSource());
    transition.playFromStart();
    event.consume();
  }

}
导入javafx.animation.animation.Status;
导入javafx.animation.RotateTransition;
导入javafx.application.application;
导入javafx.event.EventHandler;
导入javafx.geometry.Insets;
导入javafx.scene.scene;
导入javafx.scene.input.MouseEvent;
导入javafx.scene.layout.GridPane;
导入javafx.scene.shape.Rectangle;
导入javafx.stage.stage;
导入javafx.util.Duration;
公共类主扩展应用程序{
私有轮换转换;
@凌驾
public void start(Stage primaryStage)引发异常{
GridPane root=新的GridPane();
根。设置填充(新插图(20));
根.setHgap(15);
根.setVgap(15);
EventHandler onClick=this::handleClick;
对于(int i=0;i<10;i++){
对于(int j=0;j<10;j++){
矩形rect=新矩形(50,50);
rect.setOnMouseClicked(onClick);
root.add(rect,i,j);
}
}
场景=新场景(根);
初级阶段。场景(场景);
setTitle(“动画示例”);
primaryStage.SetResizeable(假);
primaryStage.show();
}
私有void handleClick(MouseEvent事件){
if(transition==null){
转换=新的旋转转换(持续时间。秒(100));
过渡期:杨乐(90);
}else if(transition.getStatus()==Status.RUNNING){
返回;
}
setNode((矩形)event.getSource());
transition.playFromStart();
event.consume();
}
}
只有当前未运行转换时,此代码才会启动新转换。但是,请注意,此设置只允许一个磁贴在任何给定时间旋转。如果希望每个平铺有一个动画,则需要进行小的调整

import java.util.HashMap;
import java.util.Map;
import javafx.animation.Animation.Status;
import javafx.animation.RotateTransition;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Main extends Application {

  private final Map<Rectangle, RotateTransition> animations = new HashMap<>();

  @Override
  public void start(Stage primaryStage) throws Exception {
    GridPane root = new GridPane();
    root.setPadding(new Insets(20));
    root.setHgap(15);
    root.setVgap(15);

    EventHandler<MouseEvent> onClick = this::handleClick;
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) {
        Rectangle rect = new Rectangle(50, 50);
        rect.setOnMouseClicked(onClick);
        root.add(rect, i, j);
      }
    }

    Scene scene = new Scene(root);
    primaryStage.setScene(scene);
    primaryStage.setTitle("Animation Example");
    primaryStage.setResizable(false);
    primaryStage.show();
  }

  private void handleClick(MouseEvent event) {
    RotateTransition animation = animations.computeIfAbsent((Rectangle) event.getSource(), rect -> {
      RotateTransition rt = new RotateTransition(Duration.millis(100), rect);
      rt.setByAngle(90);
      return rt;
    });

    if (animation.getStatus() != Status.RUNNING) {
      animation.playFromStart();
      event.consume();
    }
  }

}
import java.util.HashMap;
导入java.util.Map;
导入javafx.animation.animation.Status;
导入javafx.animation.RotateTransition;
导入javafx.application.application;
导入javafx.event.EventHandler;
导入javafx.geometry.Insets;
导入javafx.scene.scene;
导入javafx.scene.input.MouseEvent;
导入javafx.scene.layout.GridPane;
导入javafx.scene.shape.Rectangle;
导入javafx.stage.stage;
导入javafx.util.Duration;
公共类主扩展应用程序{
私有最终贴图动画=新建HashMap();
@凌驾
public void start(Stage primaryStage)引发异常{
GridPane root=新的GridPane();
根。设置填充(新插图(20));
根.setHgap(15);
根.setVgap(15);
EventHandler onClick=this::handleClick;
对于(int i=0;i<10;i++){
对于(int j=0;j<10;j++){
矩形rect=新矩形(50,50);
rect.setOnMouseClicked(onClick);
root.add(rect,i,j);
}
}
场景=新场景(根);
初级阶段。场景(场景);
setTitle(“动画示例”);
primaryStage.SetResizeable(假);
primaryStage.show();
}
私有void handleClick(MouseEvent事件){
RotateTransition animation=animations.computeFabSent((矩形)事件.getSource(),rect->{
旋转变换rt=新的旋转变换(持续时间为毫秒(100),矩形);
rt.yangle(90);
返回rt;
});
if(animation.getStatus()!=Status.RUNNING){
动画。playFromStart();
event.consume();
}
}
}

现在,每个磁贴都有自己的
旋转转换
,但动画只有在尚未运行时才会播放。

将该标记设为
原子布尔对象是否是一个好主意?或者这是不必要的?一个全局存储的常规布尔值应该可以正常工作。太好了!这么简单,但我自己解决不了。非常感谢你!
import java.util.HashMap;
import java.util.Map;
import javafx.animation.Animation.Status;
import javafx.animation.RotateTransition;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Main extends Application {

  private final Map<Rectangle, RotateTransition> animations = new HashMap<>();

  @Override
  public void start(Stage primaryStage) throws Exception {
    GridPane root = new GridPane();
    root.setPadding(new Insets(20));
    root.setHgap(15);
    root.setVgap(15);

    EventHandler<MouseEvent> onClick = this::handleClick;
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) {
        Rectangle rect = new Rectangle(50, 50);
        rect.setOnMouseClicked(onClick);
        root.add(rect, i, j);
      }
    }

    Scene scene = new Scene(root);
    primaryStage.setScene(scene);
    primaryStage.setTitle("Animation Example");
    primaryStage.setResizable(false);
    primaryStage.show();
  }

  private void handleClick(MouseEvent event) {
    RotateTransition animation = animations.computeIfAbsent((Rectangle) event.getSource(), rect -> {
      RotateTransition rt = new RotateTransition(Duration.millis(100), rect);
      rt.setByAngle(90);
      return rt;
    });

    if (animation.getStatus() != Status.RUNNING) {
      animation.playFromStart();
      event.consume();
    }
  }

}