0

在玩完我的线程游戏然后点击应用程序图标然后再次点击我的应用程序图标后回家后,我收到“挖掘机意外退出错误”,它让我回到一般应用程序屏幕,我可以点击我的应用程序图标再次在这一点上,它把我带到了游戏中并且运行良好。我希望当我重新启动我的应用程序时,我应该对线程做一些事情来防止出现令人讨厌的消息。也许在一个onStoponPause方法。这是我的线程代码片段:

getHolder().addCallback(
    new SurfaceHolder.Callback() {
        //@Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            boolean retry = true;
            gameLoopThread.setRunning(false);
            while (retry) {
                try {
                    gameLoopThread.join();
                    retry = false;
                }
                catch (InterruptedException e) {}
            }
        }

        //@Override
        public void surfaceCreated(SurfaceHolder holder) {
            screenWidth =gameView.getWidth(); 
            screenHeight = gameView.getHeight(); 
            createSprites();
            gameLoopThread.setRunning(true);
            gameLoopThread.start();
        }

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

我有一个游戏线程类:

import android.graphics.Canvas;
import android.view.SurfaceHolder;
public class GameLoopThread extends Thread {

    static final long FPS = 10;
    private GameView view;
    private boolean running = false;
    public GameLoopThread(GameView view) {
        this.view = view;
    }
    public void setRunning(boolean run) {
        running = run;
    }

    @Override
    public void run() {
        long ticksPS = 200 / FPS;
        long startTime;
        long sleepTime;
        while (running) {
            Canvas c = null;
            startTime = System.currentTimeMillis();
            try {
                c = view.getHolder().lockCanvas();
                synchronized (view.getHolder()) {
                    view.onDraw(c);
                }
            } finally {
                if (c != null) {
                    view.getHolder().unlockCanvasAndPost(c);
                }
            }
            sleepTime = ticksPS - (System.currentTimeMillis() - startTime);
            try {
                if (sleepTime > 0)
                    sleep(sleepTime);
                else
                    sleep(10);
            } catch (Exception e) {}
        }
    }
}

我尝试的一件事是将surfaceDestroyed方法更改为just;

public void surfaceDestroyed(SurfaceHolder holder) {
  gameLoopThread.setRunning(false);
}

但这没有帮助。

4

0 回答 0