【问题标题】:Create Dropping Effect for colored discs in Connect4 game Javafx在 Connect4 游戏 Javafx 中为彩色光盘创建掉落效果
【发布时间】:2021-10-03 11:04:05
【问题描述】:

我正在尝试为 Connect4 游戏创建 JavaFx 应用程序。我需要一些建议来显示彩色圆盘如何落在每列中,在每个白色部分显示彩色圆盘以快速跨度,然后进一步下降。

我创建了 7 列 VBox,每列内有 6 个圆圈。然后所有列都嵌入到一个巨大的 HBox 中。

所以当玩家点击一列时,圆盘会从顶部掉下来。为了显示我尝试过的光盘掉落:

protected boolean fillColumnWithPlayerColor(VBox col) {

    ObservableList<Node> discs = col.getChildren();

    for (int i = discs.size() - 1; i >= 0; i--) {
        Circle circle = (Circle) discs.get(i);

        if (circle.getFill().equals(Color.WHITE)) {
            if (i > 0) {
                for (int j = 0; j < i; j++) {
                    Circle prevCircle = (Circle) discs.get(j);//Assigning the current disc to player color
                    prevCircle.setFill(playerColor); //playerColor -> Color chosen by player
                    try {
                        Thread.sleep(800); //Delay for display
                        prevCircle.setFill(Color.WHITE); //Setting the color back to White
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }
            }
            circle.setFill(playerColor);
            return true;

        }

    }

    return false;

}

但无论延迟持续时间是多少(Thread.sleep),光盘只显示在总持续时间后的最终位置,而不是“掉落”效果。

有什么建议吗?

【问题讨论】:

  • 您不能为此使用 Thread.sleep。这样做会阻止 JavaFX 处理事件,包括绘制事件和处理用户输入。请改用animation

标签: java javafx-8


【解决方案1】:

你不应该在 JavaFX 中使用 Thread.sleep()。您正在为您的圈子定义一条路径,因此您应该使用 Path Transition 包。

 import javafx.animation.transition.*;
 ...
      Path path = new Path();
      path.getElements().add(new MoveTo(0f, 50f));
      pathTransition.setDuration(Duration.millis(800));
      pathTransition.setNode(circle);
      pathTransition.setPath(path);
      pathTransition.play();

如需更多帮助,请联系the documentation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-22
    • 1970-01-01
    • 1970-01-01
    • 2012-10-19
    • 1970-01-01
    相关资源
    最近更新 更多