简单的游戏循环不循环?

时间:2013-04-25 07:18:45

标签: android

我正在为简单的游戏做一个简单的游戏循环,但我只看了很短的时间。似乎游戏循环没有运行。我有布尔值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);        
    }
}

1 个答案:

答案 0 :(得分:0)

据我所知,你从未开始运行gameLoop的线程。

所以你需要这样的地方

Thread th = new Thread(gameLoop);
th.start();
祝你好运!