我想在使用 TaskTimer 的游戏循环中创建一个简单的暂停功能,但它不起作用。任务计时器有问题吗?我应该使用另一个实用程序吗?我已经坚持了一段时间了。
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
public class test extends JFrame implements KeyListener, Runnable {
public boolean pause = false;
public test() {
new JFrame();
addKeyListener(this);
setVisible(true);
}
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
if (pause) {
System.out.println("Paused");
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
System.out.println("Some Pause Related Task");
pause = false;
}
}, (long) 3000);
}
}
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if (c == KeyEvent.VK_SPACE) {
System.out.println("typed space");
pause = true;
}
}
public static void main(String[] args) {
new test();
}
}