2013-04-25 188 views
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);   
    } 
} 

回答

0

至於我可以看到你永遠不會開始運行你的GameLoop的線程。

所以地方,你需要這樣的

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

祝你好運!

+0

感謝您的回答,但我在GameLoop類的resume方法中執行此操作。這還不夠!? – 2013-04-25 08:41:07

+0

啊,我的錯。你有什麼在你的logcat? – RandomSort 2013-04-25 09:03:20

+0

logcat沒有錯誤! – 2013-04-25 09:07:13