在 Java 中执行之前让单个 if 语句等待?

发布于 2024-12-07 18:18:38 字数 998 浏览 1 评论 0原文

我试图让 Java 在评估 if 语句之前等待 1 秒,看看它是否应该更改布尔值。

我设置了如果矩形 r 与矩形 y 相交,则布尔值“相交”变为 true 并且 r 向后移动。 1 秒后,我想要一个 if 语句来检查 r 是否仍然与 y 相交。如果不是,“Intersect”再次变为 false,并且 r 停止向后移动。我该怎么做?这些语句位于同一个线程中,并且涉及动画,因此 Thread.sleep() 不起作用,因为它使动画非常跳跃。

以下是与问题相关的代码片段:

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class Game extends JFrame {

boolean p1Intersect=false;

private class Move1 extends Thread {
    public void run() {

if((p1horizontal.intersects(grass1)&&(hleft==true||hright==true)&&p1Intersect!=true) {  //Normal speed
        p1Speed = 5;
    }
if(p1horizontal.intersects(edge2)&&hleft==true) { //moves backwards after collision as long as p1Intersect = true
        p1Speed=-4;
        p1Intersect=true;
    }
if((p1horizontal.intersects(edge2)&&hleft==true)==false) { //This is the statement that I would like to wait for 1 second before executing
p1Intersect=false;
    }

I am trying to make Java wait for 1 second before evaluating an if statement to see if it should change a boolean.

I have it set up where if Rectangle r intersects Rectangle y, a boolean "Intersect" becomes true and r moves backwards. 1 second later, I want an if-statement to check if r is still intersecting y. If not, "Intersect" becomes false again and r stops moving backwards. How can I do this? These statements are in the same Thread and there is animation involved so Thread.sleep() hasn't worked as it makes the animation very jumpy.

Here is the snippets of code that are relevant to the problem:

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class Game extends JFrame {

boolean p1Intersect=false;

private class Move1 extends Thread {
    public void run() {

if((p1horizontal.intersects(grass1)&&(hleft==true||hright==true)&&p1Intersect!=true) {  //Normal speed
        p1Speed = 5;
    }
if(p1horizontal.intersects(edge2)&&hleft==true) { //moves backwards after collision as long as p1Intersect = true
        p1Speed=-4;
        p1Intersect=true;
    }
if((p1horizontal.intersects(edge2)&&hleft==true)==false) { //This is the statement that I would like to wait for 1 second before executing
p1Intersect=false;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瑾兮 2024-12-14 18:18:38

原则上,您想要做的是设置一个计时器,该计时器将在检测到第一个事件后一秒触发。如果该事件触发时,请检查矩形是否仍然相交。如果是这样,请立即采取您需要的任何行动。

正如您所注意到的,Thread.sleep() 将停止整个线程的执行,并且不会发生任何其他事情。这也会让你的动画变得紧张。

In principle, what you want to do is set a timer that will fire one second after your first event is detected. If, when that event fires, check to see whether your rectangles are still intersecting. If so, take whatever action you need at that point.

As you noticed, Thread.sleep() will halt the execution of your whole thread and nothing else will happen there. That will also make your animation jumpy.

如梦亦如幻 2024-12-14 18:18:38

在游戏设计中,尝试将逻辑保留在主线程中(也就是说,我会避免使用计时器)。我建议以下解决方案:

当检测到交叉点时,将系统时间存储在一个长的,
使用long intersectTime = System.currentTimeMillis()。

现在,在第二个 if 语句中,只需检查额外的条件
System.currentTimeMillis()-intersectTime>1000
因为 1000 毫秒 = 1 秒。

In game design, try keeping logic in main thread (that is, I would avoid using a timer). I suggest the following solution:

When intersection is detected, store the system time in a long,
using long intersectTime = System.currentTimeMillis().

Now, in the second if statement, just check for the extra condition
System.currentTimeMillis()-intersectTime>1000
since 1000 milliseconds = 1 second.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文