2

为了运行我在 Android 上创建的游戏,我一直在使用包含方法 onDraw() 和 onTouch() 的 GamePanel。我也一直在使用一个 GameThread 类,它反复调用 onDraw() 和 update()。

我的活动实例化了 GamePanel,而我的 GamePanel 实例化了 GameThread。

我的 onPause() 只是将线程内的 while 循环中的条件设置为 false。我的 onResume() 过去只是将其设置为 true,但是每次我尝试恢复应用程序时都会出现强制关闭错误(请参阅编辑 #1 )。

因此,为了解决问题,我只是重新实例化了线程,

thread = new GameThread(getHolder(), this);

这解决了部分问题,允许我最小化应用程序,然后重新打开它而没有问题。但是,当我在应用程序中锁定手机然后解锁(打开和关闭屏幕)时,线程永远不会启动。这只是我的游戏处于冻结状态。

我知道我对这个问题的解决方案非常时髦,我很想学习一种更容易接受和更干净的方式来完成这件事。

这是代码(我省略了几行似乎不相关的代码):

游戏活动:

void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    gamePanel = new MainGamePanel(this);
    setContentView(gamePanel);

}
protected void onPause() {
    super.onPause();
    gamePanel.shutDown();

}
protected void onResume() {
    super.onResume();
    gamePanel.startUp();
}

游戏面板

public MainGamePanel(Context context) {
    super(context);
    getHolder().addCallback(this);                  //adding the callback (this) to the surface  holder to intercept events
    thread = new GameThread(getHolder(), this);     // create the game loop thread
    setFocusable(true);                             // make the GamePanel focusable so it can handle events
}
public void startUp() {
    thread = new GameThread(getHolder(), this);
}

public void shutDown() {
    thread.setRunning(false);
}

游戏线程

public GameThread(SurfaceHolder surfaceHolder, MainGamePanel gameView) {
    this.surfaceHolder = surfaceHolder;
    this.gameView = gameView;
    this.run = true;
}
public void setRunning(boolean run) {
    this.run = run;
}
public void run() {
    Canvas c;//
    while (run) {
        c = null;
        try {
            c = surfaceHolder.lockCanvas(null);
            synchronized (surfaceHolder) {                  
                gameView.update();
                gameView.onDraw(c);
            }
        } finally {
            if (c != null) {
                surfaceHolder.unlockCanvasAndPost(c);
            }
        }
    }
}

如果您需要更多信息或如何使我的问题更清楚,请告诉我(另外,我是否以可理解的方式展示了我的代码?)。

一点帮助将大有帮助。如果您能提供任何建议,请提前致谢。

--编辑#1--

当我强制关闭时,这是我的日志猫错误。

12-23 14:01:34.288: E/AndroidRuntime(9484): java.lang.IllegalThreadStateException: Thread already started.

--编辑#2--

我尝试了您所说的并将我的 run() 方法更改为:

@Override
public void run() {
    Canvas c;
    while (true) {
        if (run) {/* run game thread */
            c = null;
            try {
                c = surfaceHolder.lockCanvas(null);
                synchronized (surfaceHolder) {
                    gameView.update();
                    gameView.onDraw(c);
                }
            } finally {
                if (c != null) {
                    surfaceHolder.unlockCanvasAndPost(c);
                }
            }
        } else {
            try {
                Thread.sleep(100);
            } catch (Exception e) {
            }
        }
    }
}

当我锁定我的手机时它起作用了,但是当我最小化应用程序并重新打开它时,我看到一个黑屏,几秒钟后一个弹出窗口显示“MyApp 没有响应。你想关闭它吗?”。我检查了它到达的位置,它似乎到达了 onPause() 和 shutDown(),但它从来没有到达 onResume();。此外,有时它甚至在调用 onPause() 或 shutdown() 之后调用 update(),这很奇怪。(从 LogCat 获得信息)

4

0 回答 0