各位大侠,一下代码要怎样才能使得游戏可以暂停又恢复,我知道暂停只要在线程里的循环里给一个判断,然后continue;但是怎么恢复画面呢
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
PaintThread tt = new PaintThread();
if (e.getActionCommand().equals("NewGame")) {
new TankClient(); //游戏主版面构造
}
else if (e.getActionCommand().endsWith("Stop")) {
// b=1;
// tt.run();
System.out.println("暂停!");
}
else if (e.getActionCommand().equals("Continue")) {
System.out.println("继续");
// b = 2;
// tt.run();
} else if (e.getActionCommand().equals("Exit")) {
System.out.println("退出");
System.exit(0);
} else if (e.getActionCommand().equals("help")) {
// MyHelpPanel mhp = new MyHelpPanel();
// this.remove(msp);
// this.add(mhp);
// this.setVisible(true);
}
}
private class PaintThread implements Runnable {
public void run() {
// TODO Auto-generated method stub
while (true) {
// if(变量)continue;
repaint(); //重画画面
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
PaintThread tt = new PaintThread();
if (e.getActionCommand().equals("NewGame")) {
new TankClient(); //游戏主版面构造
}
else if (e.getActionCommand().endsWith("Stop")) {
// b=1;
// tt.run();
System.out.println("暂停!");
}
else if (e.getActionCommand().equals("Continue")) {
System.out.println("继续");
// b = 2;
// tt.run();
} else if (e.getActionCommand().equals("Exit")) {
System.out.println("退出");
System.exit(0);
} else if (e.getActionCommand().equals("help")) {
// MyHelpPanel mhp = new MyHelpPanel();
// this.remove(msp);
// this.add(mhp);
// this.setVisible(true);
}
}
private class PaintThread implements Runnable {
public void run() {
// TODO Auto-generated method stub
while (true) {
// if(变量)continue;
repaint(); //重画画面
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
如
成员变量
boolean running = true;
boolean active = true;
public void run() {
while (running) {
if (active) {
//这里做相关的处理
}
yield();
}
}public void suspendGame() {
active = false;
}
public void resumeGame() {
active = true;
}
public void endGame() {
running = false;
}在你的按钮事件中调用相应的方法,如
} else if (e.getActionCommand().endsWith("Stop")) {
your_thread.suspendGame();
} else if (e.getActionCommand().equals("Continue")) {
your_thread.resumeGame();
}