安卓 : game loop pause/resume issue

标签 android multithreading surfaceview game-engine android-lifecycle

我可以通过让我的游戏线程(保持/与 surfaceview 同步)wait/notifyAll 来暂停/恢复我的游戏。使用游戏中的暂停按钮,这一切都运行良好。

但是,当我点击主页/后退按钮时,我可以暂停我的游戏,但是当我通过点击它的图标继续我的游戏时,我收到返回给我的非响应游戏屏幕。

我已经将日志放在 OnResume() 方法上,但 LogCat 中没有打印任何内容! 如果我点击我的游戏屏幕,我会在我的游戏 Activity 中出现“强制关闭”或“等待”的错误对话框。

为什么我看到这个无响应的屏幕,就好像我的应用程序在暂停后挂起一样? 如何以与游戏中暂停/恢复按钮相同的方式处理物理按钮?

这是我对操作的 logcat View 。你可以看到 onResume sop 没有像 onPause 一样被打印出来。

LogCat View

编辑:我的暂停/恢复按钮和 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 之后,您的 SurfaceView 被再次创建,但是它丢失了之前的所有数据(onDraw 所需的所有信息)。您必须将数据保存在 surfaceDestroyed 或之前,并在您的 surfaceview 的 onCreate 中重新加载它

关于安卓 : game loop pause/resume issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12686912/

相关文章:

android - 在 Android 中进行保护后,美元符号在字段中

android - 无法将 'com.android.volley.ServerError' 转换为 'com.android.volley.NoConnectionError'

java - 在Java中并行迭代单个列表而不重复

java.io.RandomAccessFile 可扩展性(或其他选项)

android - MediaCodec 和 TextureView 的 Z 顺序问题

java - Java 中日历的字符串时间戳?

android - 网络连接可以在省电模式下停止,并出现 java.net.SocketTimeoutException

multithreading - Perl线程缓慢消耗内存

android - 为什么 lockCanvas() 很慢?

android - 如何缩放图像以使其占据整个屏幕