我正在尝试让Java在评估if语句之前等待1秒钟,以查看它是否应该改变boolean值。我已经设置了一个条件,如果矩形r与矩形y相交,则布尔值“Intersect”变为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;
}