Adding pause/resume support in an Android Game
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 t开发者_Python百科he use chooses resume option? Any suggestions/ideas would be of great help.
Try looking at the source for ReplicaIsland or the sample code in the LunarLander example. These two are popular examples for starting points.
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 {
// ...
}
}
}
That's a modified version of the run() function in the LunarLander example.
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.
I think this is more or less what @vivek was describing.
I guess, the thread is updating the GUI. You can use a boolean to indicate whether current state is paused or not. Within the thread loop, you can add a condition to check whether current state is paused or not. If paused, you can skip the loop using continue. Otherwise, execute the entire code .
精彩评论