design-patterns - JavaFx 2.0 方 block 游戏动画/代码设计

标签 design-patterns javafx-2

为了保持我自己的编码技能,我最近一直在构建一个版本的 this game (Blockblaster) 在 Java 中使用 JavaFx 2.0。因为它只是为了我的利益,所以没有真正考虑软件模式或设计,所以所有的游戏逻辑最终都在 GUI 类中,随着我添加功能,它变得越来越臃肿。我最终决定重构代码库,将游戏逻辑和模型与演示(GUI)分开。

经过一番研究,我决定使用 MVC 或 MVP 之类的东西。在这样做的过程中,我决定动画(方块在触发时向上滑动游戏网格,方块在从游戏中移除时闪烁等)是 View 层的一部分。

这导致的问题是,当用户启动一个块并且 Controller 告诉 View 移动块时,它会创建 JavaFx timeline用于动画和调用 timeline.play() .这样做不会导致程序流在动画发生时在 View 中暂停,因此 View 方法返回刚刚开始动画意味着 Controller 然后继续检查以查看该块是否已构成一组块如果是这样,在移动动画到达任何地方之前删除它们。

在旧的(讨厌的)实现中,我使用了 timeline.onFinish动画完成后调用块组检查,但作为 timeline现在在 Controller 中的 View 和检查功能中,我不知道如何将其放入我的新设计中。

有没有一种方法可以等待 JavaFx 动画完成(不使应用程序线程休眠),或者我应该使用不同的设计模式来帮助避免这些问题吗?

来自 Controller 的代码

public void fire()
{   
    //Get the current column the launcher is in.
    int x = launcher.getX(), startY = launcher.getY();
    //Find the next available block in the column.
    int endY;
    for(endY = h; endY > 0 && blockMap[endY - 1][x] == null; endY--){}

    //Create a new block of the same colour and location as that on the launcher.
    addBlock(x, launcher.getY(), getCurrentColourAndRotate());
    //Move the block in the GUI and model (this will trigger the animation in the GUI)
    moveBlock(x, startY, x, endY);

    //Remove any block groups that have been made.
    checkBlock(blockMap[endY][x]);
    //Remove any blocks now not connected to the top of the game grid
    removeUnconnectedBlocks();
}

示例游戏截图

Sample game picture
(来源:myhappygames.com)

最佳答案

好的,所以这是匆忙完成的,但是假设 Block 是您按下的方块,而 Group 是 3 个或更多与我的绘图中颜色相同的相邻块的集合,那么您遇到的麻烦是您用同样的方法做的太多了。

enter image description here

enter image description here

当你按下一个块时,你应该通知你的 Controller 。这应该决定这是否是一个组的一部分,如果是这样就让该组着火? (我不熟悉术语)。该组中的块应该被赋予一个新状态,例如 ON_FIRE。模型将通知所有观察者这些块现在处于 ON_FIRE。一旦 View 获得此信息,您将执行 ON_FIRE 动画、折叠或其他任何操作。一旦完成,这应该对应于 timeLine.onFinnish ,您现在调用 Controller 中的下一个方法,例如 completedBurning .现在这将负责清理和间接触发新事物。

希望这是有道理的。

Class diagram

关于design-patterns - JavaFx 2.0 方 block 游戏动画/代码设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13327461/

相关文章:

javafx tableview 未从 fxml 加载(为 null)

FXML 中的 JavaFX MediaView

tableview - JavaFX TableView 复制到剪贴板

c++ - 设计问题 : apply specific treatment to a subclass only manipulating the superclass

java - 如何在没有instanceof或类型转换的情况下以OOP方式防止继承破坏所有依赖关系?

c# - WPF 自定义控件设计模式。 MVVM?

Java:使单例不那么冗长

c# - 指定我需要一个实现 2 个或更多接口(interface)的对象的接口(interface)引用

JavaFX2.2(稳定)忽略 "socksProxyHost"和 "socksProxyPort"的设置属性?

java - javafx media可以播放mp4格式的视频文件吗?