添加暂停/继续支持在Android游戏 [英] Adding pause/resume support in an Android Game

查看:202
本文介绍了添加暂停/继续支持在Android游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想补充暂停/恢复到Android游戏的支持。如何暂停当前线程,当用户presses暂停选项,并恢复该线程使用时选择恢复选项?任何建议/想法会有很大的帮助。

I am trying to add Pause/Resume support to my Android game. How do I pause the current thread when user presses pause option and resume the thread when the use chooses resume option? Any suggestions/ideas would be of great help.

推荐答案

尝试看来源的 ReplicaIsland 或在样品code <一个href=\"http://developer.android.com/resources/samples/LunarLander/src/com/example/android/lunarlander/LunarView.html\"相对=nofollow> LunarLander 例子。这两个是起点最典型的例子。

Try looking at the source for ReplicaIsland or the sample code in the LunarLander example. These two are popular examples for starting points.

此外,请确保您了解如何线程工作,如果你真的要暂停的线程。另一种方法是简单地暂停绘图/更新,而线程仍然运行。有些code像这可能是你在找什么:

Also, make sure you understand how threads work and if you really want to pause the thread. Another approach would be to simply pause the drawing / updating while the thread still runs. Some code like this may be what you're looking for:

@Override
public void run()
{
    while (mRun)
    {
        Canvas c = null;
        try
        {
            c = mSurfaceHolder.lockCanvas(null);
            synchronized (mSurfaceHolder)
            {
                if (mMode == STATE_RUNNING)
                {
                  updatePhysics();
                }
                doDraw(c);
            }
        } finally {
          // ...
        }
    }
}

这是在LunarLander示例的run()函数的一修改的版本。

That's a modified version of the run() function in the LunarLander example.

主要关心的一点是,其中up​​datePhysics()函数只被调用,如果mMode服务== STATE_RUNNING。简单地改变mMode服务会导致物理停止更新并有效地暂停游戏。

The main point of interest is where the updatePhysics() function only gets called if mMode == STATE_RUNNING. Simply changing mMode will cause the physics to stop updating and effectively pause the game.

我觉得这是多了还是少了什么@vivek被描述的。

I think this is more or less what @vivek was describing.

这篇关于添加暂停/继续支持在Android游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆