簡體   English   中英

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

[英]How to properly put onPause() and onResume() methods in my game

我正在以表面視圖方式制作游戲,而我要實現的目標是如果按下主屏幕按鈕,並且在再次打開游戲時暫停游戲,則從停止游戲的確切位置恢復

我在這里查看了幾個類似的問題,但沒有一個給我我想要的東西,也許是因為我們的代碼結構不同

我的代碼在下面添加,我刪除了我認為不相關的代碼的很大一部分,但是如果有人需要,我會添加它

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();

}
}

我試圖為onPause()設置thread.setRunning(false),但是游戲崩潰了

請有人可以指導我解決方案

編輯2

當按下HOME時,應用程序進入后台,我認為該應用程序無法進一步運行(除非您在代碼中指定),並且當該應用程序進入前台時,其狀態將與進入后台之前的狀態相同,除非系統已終止獲取資源。

看到:

因此,應在按下HOME鍵時暫停游戲,並且其狀態應與暫停前的狀態相同(如果尚未被電話銷毀)。

您需要確保您的應用程序數據在后台時不會被破壞。 我無法為您提供幫助,因為我沒有android開發方面的經驗,但是也許此鏈接可以幫助您:

Android應用程序在后台運行時保存數據

PS 我會刪除所有以前的答案,因為我誤解你和他們有你需要什么沒有關系。 (完成)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM