如何在我的游戏中正确放置onPause()和onResume()方法

时间:2017-06-11 19:21:02

标签: java android

我在表面视图中制作游戏,我想要实现的是在按下主页按钮时暂停游戏并且再次打开游戏时从游戏停止的确切位置恢复

我在这里看过几个类似的问题,没有人给我我想要的东西,也许是因为我们的代码结构不同

我的代码添加在下面,我删除了一些我认为不相关的代码的好部分,但是如果有人需要它我会添加它

GamePanel.java

@Override
public void surfaceCreated(SurfaceHolder holder){

    thread = new MainThread(getHolder(), this);
    //we can safely start the game loop
    thread.setRunning(true);
    thread.start();
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){}

@Override
public void surfaceDestroyed(SurfaceHolder holder){
    boolean retry = true;
    int counter = 0;
    while(retry && counter<1000)
    {
        counter++;
        try{thread.setRunning(false);
            thread.join();
            retry = false;
            thread = null;
        }catch(InterruptedException e){e.printStackTrace();}

    }

}

public void update()

{
    if (playing) {
        double fuelUsed = (player.getFuel() / PHYS_FUEL_MAX ) * thread.UI_BAR;

        if (fuelUsed > thread.mFuel) {
            fuelUsed = thread.mFuel;
        }
        thread.mFuel -= fuelUsed;
    }

        if(player.getPlaying()) {

        if(botborder.isEmpty())
        {
            player.setPlaying(false);
            return;
        }

        bg.update();
        player.update();
        fuelsmall.update();
        fuelbig.update();
        fuelnegative.update();
        if(collectFuelSmall(player, fuelsmall)){
            player.fuel +=500;
        }
        if(collectFuelBig(player, fuelbig)){
            player.fuel +=1000;
        }
        if(collectFuelNegative(player, fuelnegative)){
            player.fuel -= 250;
        }
    else{
        player.resetDY();
        if(!reset)
        {
            newGameCreated = false;
            startReset = System.nanoTime();
            reset = true;
            disappear = true;
            explosion = new Explosion(BitmapFactory.decodeResource(getResources(),R.drawable.explosion),player.getX(),
                    player.getY()-30, 100, 100, 25);
            player.fuel=3500;
        }
    if (player.getScore() > getBestScore()) {
        setBestScore(player.getScore());
    }
    }

MainThread

public MainThread(SurfaceHolder surfaceHolder, GamePanel gamePanel)
{
    super();
    this.surfaceHolder = surfaceHolder;
    this.gamePanel = gamePanel;

}


@Override
public void run()
{
    long startTime;
    long timeMillis;
    long waitTime;
    long totalTime = 0;
    int frameCount =0;
    long targetTime = 1000/FPS;

    while(running) {
        startTime = System.nanoTime();
        canvas = null;

        //try locking the canvas for pixel editing
        try {
            canvas = this.surfaceHolder.lockCanvas();
            synchronized (surfaceHolder) {


                this.gamePanel.update();
                this.gamePanel.draw(canvas);
            }
        } catch (Exception e) {
        }
        finally{
            if(canvas!=null)
            {
                try {
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
                catch(Exception e){e.printStackTrace();}
            }
        }




        timeMillis = (System.nanoTime() - startTime) / 1000000;
        waitTime = targetTime-timeMillis;

        try{
            this.sleep(waitTime);
        }catch(Exception e){}

        totalTime += System.nanoTime()-startTime;
        frameCount++;
        if(frameCount == FPS)
        {
            averageFPS = 1000/((totalTime/frameCount)/1000000);
            frameCount =0;
            totalTime = 0;
            System.out.println(averageFPS);
        }
    }
}
public void setRunning(boolean b) {
    running=b;
}
}

Game.java

public class Game extends Activity {

protected void onPause() {
    super.onPause();
}
protected void onResume() {
    super.onResume();

}
}

我试过设置thread.setRunning(false),  对于onPause()但游戏崩溃

请有人指导我找到解决方案

1 个答案:

答案 0 :(得分:0)

编辑2

当按下HOME时,应用程序进入后台,我认为应用程序不会进一步运行(除非您在代码中指定了该应用程序),并且当应用程序进入前景时,其状态将与进入后台之前的状态相同,除非系统已将其杀死以获取资源。

见:

因此,当按下HOME时,您的游戏应暂停,并且如果手机尚未销毁,则状态应与暂停前的状态相同。

您需要确保您的应用数据在后台不会被销毁。我无法帮助你,因为我没有使用android开发经验,但也许这个链接可能有所帮助:

Android Application saving data when app in background

P.S。 我会删除所有以前的答案,因为我误解了你,他们与你需要的东西毫无关系。 (完成)