【发布时间】:2012-10-02 08:31:28
【问题描述】:
我可以通过让我的游戏线程(与surfaceview 保持/同步)等待/通知所有来暂停/恢复我的游戏。使用游戏中的暂停按钮,这一切运行良好。
但是,当我点击主页/返回按钮时,我可以暂停我的游戏,但是当我通过点击图标恢复我的游戏时,我收到了无响应的游戏画面。
我已将日志放在 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: 如果我使用暂停按钮暂停我的游戏并让我的手机自动锁定......恢复后我可以正常玩游戏而没有任何问题。 根据日志,我看到在这种情况下不会调用表面破坏。
【问题讨论】:
-
您的 OnResume 和 OnPause 代码是怎么说的?
-
在 EDIT 下查看我对帖子的更新
-
我假设您暂停游戏的方法以某种方式在 UI 线程上“休眠”。这不是这样做的方法。向我们展示游戏暂停时执行的代码
-
我已经在帖子中添加了代码,请帮忙!
标签: android multithreading surfaceview game-engine android-lifecycle