I'm trying to make a while
loop that increments a double
by 0.1 once every second:
public void keyReleased(KeyEvent e)
{
//When a key is released, look for any collision problems
airOrGround(xPos, yPos);
collision(xPos, yPos);
//As long as onGround (which is set to false or true in airOrGround) is false,
//yPos should increment by 1 every second
while(onGround == false)
{
try {
Thread.sleep(1*1000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
yPos += 0.1;
yPos = (Math.round(yPos * 10) / 10.0);
start.setText("X = " + xPos + ", Y = " + yPos);
airOrGround(xPos, yPos);
}
}
Once I run it, as soon as the keyReleased()
runs, the program freezes. I've also tried putting the while
loop inside the try
, but that doesn't work either. No errors appear in the console and without the Thread.sleep
part it doesn't freeze