- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的棋盘游戏取得进展,因此只需单击鼠标,就会执行一些任务,例如播放一些动画、接收棋盘的新 Pane (我问了我的最后一个问题,现在效果很好)、对玩家进行更改数据等
所以,这是我的事件处理程序
grid.setOnMouseReleased(new EventHandler<MouseEvent> () {
public void handle(MouseEvent me) {
//make changes to player data
//receive new panes for the board
//make some gui changes
//play some animations
}
});
这里grid
是一个 GridPane 对象,其中包含板中每个单元格的其他 Pane 、动画 Pane 等。
处理一个鼠标事件需要 2-3 秒。在此过程中,我看到另一次鼠标单击也将开始与已经正在进行的鼠标单击并行处理。
我想要的是在第一个鼠标事件完成之前不应处理另一个鼠标事件。如果接收到的任何点击都被丢弃,那就更好了。
我尝试使用线程:
grid.setOnMouseReleased(new EventHandler<MouseEvent> () {
public void handle(MouseEvent me) {
Thread t = new Thread(new Runnable() {
public void run() {
//completing the necessary tasks here
}
});
t.start();
t.join();
}
});
但是如果我使用线程,GUI 更改就不会发生(不知道为什么,我用很多不同的方式测试了它,总是无法进行 GUI 更改),所以我猜这是行不通的。
那么我该怎么做才能在处理一个鼠标事件时不接受任何鼠标点击呢?我认为这个问题不需要更多代码,但如果有必要,我会编辑并添加一些代码。
编辑:
class Animations {
public Pane getAnimationPane() {
//returns Pane which would be used for transition
}
public void playAnimation() {
//called only when the timeline transition is to be played
}
}
因此,我创建了一个 Animations 类数组,因为我需要很多这样的 Pane ,并且需要跟踪每个 Pane 。
当我需要播放动画时,我调用playAnimation()
方法。
现在这个playAnimation()
方法就像一个动画启动。
此 Pane 上的动画完成后,会根据玩家的进度对棋盘进行更改,如果需要的话...此 playAnimation()
方法将调用 playAnimation()
几个其他动画对象的方法。
grid.setOnMouseReleased(new EventHandler<MouseEvent> () {
public void handle(MouseEvent me) {
//make changes to player data
//receive new panes for the board
//make some gui changes
//play some animations
someAnimationObject.playAnimation();
}
});
这可能深达 10-20 个(如果网格尺寸很大,则更多)其他动画对象用于其 playAnimation()
.
编辑2:
playAnimation()
的Animations
可以调用playAnimation()
在 0-8 个其他动画对象上。而且这种情况可能会继续下去。
编辑3:
我终于解决了我的问题,我就是这样做的。
我添加了SimpleBooleanProperty
isAnimating
给我的Animations
类(实例变量,即非静态),将其设置为 false
默认情况下。
当时间轴动画开始播放时,我将其设置为 true
,当时间轴动画完成时,我将其设置为 false
.
在主类中(例如 Game
),我添加了一个 BooleanBinding
anyAnimating
。我绑定(bind)了anyAnimating
至isAnimating
每个Animations
使用我说我创建的数组的对象。
anyAnimating = Bindings.or(anyAnimating, AnimationsObj[i][j].isAnimatingProperty());
最后我使用了 anyAnimating
的值正如 Brad 建议的那样,作为 MouseEvent EventHandler 中的标志。
grid.setOnMouseReleased(new EventHandler<MouseEvent> () {
public void handle(MouseEvent me) {
if(!anyAnimating.get()){
//make changes to player data
//receive new panes for the board
//make some gui changes
//play some animations
}
}
});
最佳答案
我不太确定这里的详细信息,因为您的问题中没有太多关于如何管理动画的详细信息,但这应该可以让您有一个大概的了解。
向您的 Animations
类添加一个标志,指示动画是否正在进行中:
class Animations {
private ReadOnlyBooleanWrapper animating = new ReadOnlyBooleanWrapper() ;
public ReadOnlyBooleanProperty animatingProperty() {
return animating.getReadOnlyProperty();
}
public boolean isAnimating() {
return animatingProperty().get();
}
public Pane getAnimationPane() {
//returns Pane which would be used for transition
}
public void playAnimation() {
//called only when the timeline transition is to be played
Animation animation = ... ;
animation.statusProperty().addListener((obs, oldStatus, newStatus) ->
animating.set(newStatus == Animation.Status.RUNNING));
animation.play(); // etc
}
}
现在你可以做
grid.setOnMouseReleased(new EventHandler<MouseEvent> () {
public void handle(MouseEvent me) {
if (! someAnimationObject.isAnimating()) {
//make changes to player data
//receive new panes for the board
//make some gui changes
//play some animations
someAnimationObject.playAnimation();
}
}
});
或者也许
grid.setOnMouseReleased(new EventHandler<MouseEvent> () {
public void handle(MouseEvent me) {
grid.setDisable(true);
//make changes to player data
//receive new panes for the board
//make some gui changes
//play some animations
someAnimationObject.animatingProperty().addListener((obs, wasAnimating, isNowAnimating) -> {
if (! isNowAnimating) {
grid.setDisable(false);
}
});
someAnimationObject.playAnimation();
}
});
另一种方法是让 playAnimation()
接受回调以在动画完成时执行(或者当所有动画完成时,如果您正在执行多个动画)。
这看起来像:
class Animations {
public Pane getAnimationPane(Runnable finishedCallback) {
//returns Pane which would be used for transition
}
public void playAnimation() {
//called only when the timeline transition is to be played
Animation animation = ... ;
animation.statusProperty().addListener((obs, oldStatus, newStatus) -> {
if (newStatus == Animation.Status.STOPPED) {
finishedCallback.run();
}
});
animation.play(); // etc
}
}
那么你的事件处理代码可以这样做:
// declared at instance level:
private boolean animating = false ;
grid.setOnMouseReleased(new EventHandler<MouseEvent> () {
public void handle(MouseEvent me) {
if (! animating) {
animating = true ;
//make changes to player data
//receive new panes for the board
//make some gui changes
//play some animations
someAnimationObject.playAnimation( () -> {animating = false ;});
}
}
});
或
grid.setOnMouseReleased(new EventHandler<MouseEvent> () {
public void handle(MouseEvent me) {
grid.setDisable(true);
//make changes to player data
//receive new panes for the board
//make some gui changes
//play some animations
someAnimationObject.playAnimation( () -> {grid.setDisable(false);});
}
});
关于java - 如何在接受下一个 MouseEvent 之前等待当前 MouseEvent EventHandler 结束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32925149/
我正在编写一个类,我想知道哪一对方法更适合描述流程周期: start() -> stop() start() -> end() start() -> finish() 基本上这些方法将在执行任务之前和
对于 Android 小部件类名称是否应以“View”、“Layout”或两者都不结尾,是否存在模式或命名约定? 最佳答案 如果该类扩展了 View(或在其层次结构中扩展了 View),那么它应该以“
我正在尝试找到一个插件,该插件将使用 Verilog 突出显示匹配的开始/结束语句。 VIM 让它与花括号/括号一起工作,但它不能与它的开始/结束一起工作。我希望 VIM 突出显示正确的开始到正确的结
给出以下代码: % Generate some random data n = 10; A = cell(n, 1); for i=1:n A{i} = timeseries; A{i
我需要知道是否可以检测输入何时开始聚焦以及何时结束焦点 HTML 代码: JQuery 代码(仅示例我如何需要它): $('.datas').on('focusStart', alert("fo
所以我一直在思考一款游戏的想法,一款需要穿越时空的游戏。因此,我编写了一个 JFrame 来显示螺旋的 .gif,但它并没有在对话框显示时结束,而是保留在后台。我可以解决这个问题吗? import j
给出以下使用多线程的 Java 示例: import java.util.concurrent.*; public class SquareCalculator { private Ex
好吧,我有一个 do-while 循环,应该在使用点击“q”时结束,但它给了我错误消息,请帮忙。 package Assignments; import java.util.*; public cla
我如何有选择地匹配开始 ^或结束 $正则表达式中的一行? 例如: /(?\\1', $str); 我的字符串开头和结尾处的粗体边缘情况没有被匹配。我在使用其他变体时遇到的一些极端情况包括字符串内匹配、
我试图让程序在总数达到 10 时结束,但由于某种原因,我的 while 循环在达到 10 时继续计数。一旦回答了 10 个问题,我就有 int 百分比来查找百分比。 import java.util.
jQuery 中的 end() 函数将元素集恢复到上次破坏性更改之前的状态,因此我可以看到它应该如何使用,但我已经看到了一些代码示例,例如:on alistapart (可能来自旧版本的 jQuery
这个问题在这里已经有了答案: How to check if a string "StartsWith" another string? (18 个答案) 关闭 9 年前。 var file =
我正在尝试在 travis 上设置两个数据库,但它只是在 before_install 声明的中途停止: (END) No output has been received in the last 1
我创建了一个简单的存储过程,它循环遍历一个表的行并将它们插入到另一个表中。由于某种原因,END WHILE 循环抛出缺少分号错误。所有代码对我来说都是正确的,并且所有分隔符都设置正确。我只是不明白为什
您好,我正在使用 AVSpeechSynthesizer 和 AVSpeechUtterance 构建一个 iOS 7 应用程序,我想弄清楚合成何时完成。更具体地说,我想在合成结束时更改播放/暂停按钮
这是我的代码,我试图在响应后显示警报。但没有显示操作系统警报 string filepath = ConfigurationManager.AppSettings["USPPath"].ToStri
我想创建一个循环,在提供的时间段、第一天和最后一天返回每个月(考虑到月份在第 28-31 天结束):(“function_to_increase_month”尚未定义) for beg in pd.d
我目前正在用 Python 3.6 为一个骰子游戏编写代码,我知道我的编码在这方面有点不对劲,但是,我真的只是想知道如何开始我的 while 循环。游戏说明如下…… 人类玩家与计算机对战。 玩家 1
所以我已经了解了如何打开 fragment。这是我的困境。我的 view 旁边有一个元素列表(元素周期表元素)。当您选择一个元素时,它会显示它的信息。 我的问题是我需要能够从(我们称之为详细信息 fr
我想检测用户何时停止滚动页面/元素。这可能很棘手,因为最近对 OSX 滚动行为的增强创造了这种新的惯性效应。是否触发了事件? 我能想到的唯一其他解决方案是在页面/元素的滚动位置不再改变时使用间隔来拾取
我是一名优秀的程序员,十分优秀!