【发布时间】:2019-08-11 07:40:31
【问题描述】:
所以我用 Java + Processing 制作了一个简单的游戏,其中 draw() 中有按钮和循环。显然,如果存在循环,PApplet 函数 mousePressed() 不会持续工作,因此我尝试在循环期间检查自己的 checkmouse() 函数。但是,它仍然不起作用。如何做到这一点,以便我可以运行带有 while-loops 的游戏并同时不断检查 mousePressed?
//draw() func
public void draw() {
for (int i = 0; i < 10000; i++) { //to simulate a while loop
//do something, like run some other functions that create the buttons
checkmouse();
}
}
//checkmouse function
public void checkmouse() {
if (mousePressed) {
System.out.println("x");
}
}
当我在处理窗口中单击鼠标时,即使 checkmouse() 每次循环都会运行,它也不会显示“x”,因此理论上它应该在循环运行时不断检查它。
还有人可以解释为什么这不起作用吗?
boolean esc = false;
while (!esc) {
if (mousePressed) {
System.out.println("x");
esc = true;
}
}
【问题讨论】:
-
1) 不要阻塞 EDT(事件调度线程)。发生这种情况时,GUI 将“冻结”。有关详细信息和修复,请参阅Concurrency in Swing。 2)为了尽快获得更好的帮助,edit添加minimal reproducible example或Short, Self Contained, Correct Example。
标签: java processing event-dispatch-thread