Android游戏循环线程崩溃

时间:2015-08-05 17:05:46

标签: java android loops crash android-canvas

我对编程非常陌生,而且我正试图制作游戏时遇到问题。

几乎每次我退出比赛时都会崩溃。 添加:if(canvas!= null)之前的canvas.drawColor(Color.CYAN);它不会在退出时崩溃,但是当我返回游戏时。

这是代码:(它不是我的实际游戏...... OnDraw中没有任何东西exept canvas.drawColor(Color.CYAN);这样可以保持简单)

GameView.java:

package com.example.test.threadtest;

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;
import android.view.SurfaceHolder;
import android.view.SurfaceView;


public class GameView extends SurfaceView {

    private GameLoopThread theGameLoopThread;
    private SurfaceHolder surfaceHolder;


    public GameView(Context context) {
        super(context);
        theGameLoopThread = new GameLoopThread(this);
        surfaceHolder = getHolder();


        surfaceHolder.addCallback(new SurfaceHolder.Callback() {


            public void surfaceDestroyed(SurfaceHolder holder) {

                boolean retry = true;
                theGameLoopThread.setRunning(false);
                while (retry) {
                    try {
                        theGameLoopThread.join();
                        retry = false;
                    } catch (InterruptedException e) {

                    }

                }

            }

            public void surfaceCreated(SurfaceHolder holder) {
                theGameLoopThread.setRunning(true);
                theGameLoopThread.start();
            }

            public void surfaceChanged(SurfaceHolder holder, int format,
                                       int width, int height) {
                // TODO Auto-generated method stub

            }
        });

    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (canvas != null)
            canvas.drawColor(Color.CYAN);


    }
}

GameLoopThread.java:

package com.example.test.threadtest;



import android.annotation.SuppressLint;
import android.graphics.Canvas;



public class GameLoopThread extends Thread {
    static final long FPS = 20;
    private GameView theView;
    private boolean isRunning = false;

    public GameLoopThread(GameView theView) {
        this.theView = theView;
    }

    public void setRunning(boolean run) {
        isRunning = run;
    }

    @SuppressLint("WrongCall") @Override
    public void run() {
        long TPS = 1000 / FPS;
        long startTime, sleepTime;
        while (isRunning) {
            Canvas theCanvas = null;
            startTime = System.currentTimeMillis();
            try {
                theCanvas = theView.getHolder().lockCanvas();
                synchronized (theView.getHolder()) {
                    theView.onDraw(theCanvas);
                }
            } finally {
                if (theCanvas != null) {
                    theView.getHolder().unlockCanvasAndPost(theCanvas);
                }
            }
            sleepTime = TPS - (System.currentTimeMillis() - startTime);
            try {
                if (sleepTime > 0)
                    sleep(sleepTime);
                else
                    sleep(10);
            } catch (Exception e) {

            }
        }
    }
}

MainAcitvity.java:

package com.example.test.threadtest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new GameView(this));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

0 个答案:

没有答案