通过制作我的游戏线程(与surfaceview保持/同步)wait / notifyAll,我可以暂停/恢复我的游戏 . 这一切都运行在游戏暂停按钮 .
但是,当我点击主页/后退按钮时,我可以暂停我的游戏,但是当我通过点击其图标恢复游戏时,我会收到非响应式游戏画面给我 .
我已将日志放在OnResume()方法上,但在LogCat中没有打印任何内容!如果我点击我的游戏屏幕,我会在游戏活动中收到错误对话框“强制关闭”或“等待” .
为什么我得到这个无响应的屏幕,好像我的应用程序暂停后挂起?如何以与游戏暂停/恢复按钮相同的方式处理物理按钮?
这是我的logcat操作视图 . 你可以看到onResume sop没有被打印类似于onPause .
编辑:由我的暂停/恢复按钮和onPause / onResume函数调用的代码,其中“this”是线程类本身
protected void setPause(){
synchronized (this) {
isPaused = true;
}
}
protected void setResume(){
synchronized (this) {
isPaused = false;
this.notifyAll();
}
}
游戏循环代码:
synchronized (this) {
while (running) {
if (isPaused) {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
Canvas c = null;
try {
c = view.getHolder().lockCanvas();
synchronized (view.getHolder()) {
view.onDraw(c);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (c != null) {
view.getHolder().unlockCanvasAndPost(c);
}
}
sleepTime = ticksPS
- (System.currentTimeMillis() - startTime);
try {
if (sleepTime > 0)
sleep(sleepTime);
else
sleep(10);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
这是我如何从表面视图调用它:
public void surfaceCreated(SurfaceHolder holder) {
// start the game loop
System.out.println("Starting Game Loop @@@@@@@@@");
if(isPaused){
System.out.println("GamePaused");
gameLoopThread.setResume();
}
else{
System.out.println("Game Starting");
gameLoopThread.setRunning(true);
}
gameLoopThread.start();
}
编辑2:如果我使用暂停按钮暂停我的游戏并让我的手机自动锁定...恢复后我可以正常播放没有任何问题 . 根据日志,我看到在这种情况下不会调用被破坏的表面 .