0

我正在为一个简单的游戏做一个简单的游戏循环,但我只看到屏幕很短的时间。似乎游戏循环没有运行。我有布尔值 gameRunning ,当这个值为 TRUE 时,游戏循环应该继续运行 run() 方法,但它不会那样做!我一定错过了什么,但我不能用我的眼睛看到我错过了什么!谁能看到我错过了什么才能运行这个循环?一些帮助!谢谢!

编辑:如果有帮助的话,我添加了 MainActivity 的所有代码

public class MainActivity extends Activity  implements OnTouchListener {

// Variables and references
public static int screenWidth, screenHeight;
public static float xTouch, yTouch;
private GameLoop gameLoop;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);

    // Set screen to landscape
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    // Set screen to full screen
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    // Remove title from screen
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    // Get the width and height of the screen area
    Display display = getWindowManager().getDefaultDisplay();
    screenWidth = display.getWidth();
    screenHeight = display.getHeight();

    // Create a new object of GameLoop and pass this context
    gameLoop = new GameLoop(this);
    gameLoop.setOnTouchListener(this);
    setContentView(gameLoop);
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    gameLoop.pause();
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    gameLoop.resume();
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    xTouch = event.getX();
    yTouch = event.getY();

    switch (event.getAction()) {

    case MotionEvent.ACTION_DOWN:

        break;

    case MotionEvent.ACTION_UP:

        break;

    case MotionEvent.ACTION_MOVE:

        break;
    }

    // return false;
    return true; // This gets the coordinates all the time
}
}

这是 GameLoop 类的代码:

public class GameLoop extends SurfaceView implements Runnable {

    // Variables and references
    private SurfaceHolder surfaceHolder;
    private Canvas canvas;
    private boolean gameRunning = false;
    private Thread gameThread = null;
    private Paint paint;

    // Constructor
    public GameLoop(Context context) {
        super(context);

        surfaceHolder = getHolder();

        // Create Paint object
        paint = new Paint();
    }

    public void pause() {
        gameRunning = false;
        while (true) {
            try {
                gameThread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            break;
        }
        gameThread = null;
        System.exit (0);
    }

    public void resume() {
        gameRunning = true;
        gameThread = new Thread(this);
        gameThread.start();
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub

        while (gameRunning) {
            if (!surfaceHolder.getSurface().isValid())
                continue;

        canvas = surfaceHolder.lockCanvas();

        // Call method to draw objects on screen
        drawObjects(canvas);

        surfaceHolder.unlockCanvasAndPost(canvas);
        }
    }

    // Method that draw everything on canvas
    private void drawObjects(Canvas canvas) {

        // Test
        paint.setAntiAlias(true);
        paint.setColor(Color.WHITE);

        // Clear screen with black color
        canvas.drawRGB(0, 0, 0);

        // Draw a circle
        canvas.drawCircle(100, 100, 100, paint);        
    }
}
4

1 回答 1

0

据我所知,您永远不会启动运行游戏循环的线程。

所以在某个地方你需要这样的东西

Thread th = new Thread(gameLoop);
th.start();

祝你好运!

于 2013-04-25T08:37:17.090 回答