我想增加暂停/恢复游戏的可能性。不幸的是,我的解决方案仅暂停游戏,但不恢复游戏(当我单击“恢复”按钮时,什么也没有发生-图像仍然静止)。我还尝试了调用wait / notify方法thread
,但也没有成功-我得到了Exception in thread "AWT-EventQueue-0" java.lang.IllegalMonitorStateException
。进行暂停/恢复的最佳解决方案是什么?
游戏循环
public void run() {
init();
long startTime;
long reTime;
long waitTime;
while (running) {
startTime = System.nanoTime();
int CarPosX = car.zwrocPolozenieX();
int CarPosY = car.zwrocPolozenieY();
update(b, CarPosX, CarPosY);
render(b);
//draw();
repaint();
if(b<4390) {
b=b+szybkoscMapy;
}
reTime = System.nanoTime() - startTime;
waitTime = targetTime - reTime/100000000;
try {
Thread.sleep(waitTime);
}
catch(Exception e) {
}
}
}
public void init() {
if(thread == null) {
thread = new Thread(this);
thread.start();
}
b=0;
running = true;
image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
g = (Graphics2D) image.getGraphics();
map = new Map(levelNumber);
car = new Car(map);
car.setxpos(338);
car.setypos(150);
}
听众
pause_button.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
bp.running = false;
}
});
resume_button.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
bp.running = true;
}
});
用于javax.swing.Timer
调整动画的速度。这里将介绍调用计时器start()
和stop()
方法的典型控件。该车队的仿真,使用这样的,也可能是如果利率。Timer
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句