一款非常不錯的android遊戲迴圈原理

來源:互聯網
上載者:User

解說一款非常不錯的android遊戲迴圈原理,給安卓遊戲開發的朋友。


 [代碼]DroidzActivity.java
[java]  package net.obviam.droidz; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Window; 
import android.view.WindowManager; 
 
public class DroidzActivity extends Activity { 
    /** Called when the activity is first created. */ 
 
 private static final String TAG = DroidzActivity.class.getSimpleName(); 
 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        // requesting to turn the title OFF  
        requestWindowFeature(Window.FEATURE_NO_TITLE); 
        // making it full screen  
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
        // set our MainGamePanel as the View  
        setContentView(new MainGamePanel(this)); 
        Log.d(TAG, "View added"); 
    } 
 
 @Override 
 protected void onDestroy() { 
  Log.d(TAG, "Destroying..."); 
  super.onDestroy(); 
 } 
 
 @Override 
 protected void onStop() { 
  Log.d(TAG, "Stopping..."); 
  super.onStop(); 
 } 

package net.obviam.droidz;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

public class DroidzActivity extends Activity {
    /** Called when the activity is first created. */

 private static final String TAG = DroidzActivity.class.getSimpleName();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // requesting to turn the title OFF
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // making it full screen
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        // set our MainGamePanel as the View
        setContentView(new MainGamePanel(this));
        Log.d(TAG, "View added");
    }

 @Override
 protected void onDestroy() {
  Log.d(TAG, "Destroying...");
  super.onDestroy();
 }

 @Override
 protected void onStop() {
  Log.d(TAG, "Stopping...");
  super.onStop();
 }
}

2. [代碼]MainThread.java
[java]  package net.obviam.droidz; 
 
import android.util.Log; 
import android.view.SurfaceHolder; 
 
public class MainThread extends Thread { 
 
 private static final String TAG = MainThread.class.getSimpleName(); 
 
 private SurfaceHolder surfaceHolder; 
 private MainGamePanel gamePanel; 
 private boolean running; 
 public void setRunning(boolean running) { 
  this.running = running; 
 } 
 
 public MainThread(SurfaceHolder surfaceHolder, MainGamePanel gamePanel) { 
  super(); 
  this.surfaceHolder = surfaceHolder; 
  this.gamePanel = gamePanel; 
 } 
 
 @Override 
 public void run() { 
  long tickCount = 0L; 
  Log.d(TAG, "Starting game loop"); 
  while (running) { 
   tickCount++; 
   // update game state  
   // render state to the screen  
  } 
  Log.d(TAG, "Game loop executed " + tickCount + " times"); 
 } 

package net.obviam.droidz;

import android.util.Log;
import android.view.SurfaceHolder;

public class MainThread extends Thread {

 private static final String TAG = MainThread.class.getSimpleName();

 private SurfaceHolder surfaceHolder;
 private MainGamePanel gamePanel;
 private boolean running;
 public void setRunning(boolean running) {
  this.running = running;
 }

 public MainThread(SurfaceHolder surfaceHolder, MainGamePanel gamePanel) {
  super();
  this.surfaceHolder = surfaceHolder;
  this.gamePanel = gamePanel;
 }

 @Override
 public void run() {
  long tickCount = 0L;
  Log.d(TAG, "Starting game loop");
  while (running) {
   tickCount++;
   // update game state
   // render state to the screen
  }
  Log.d(TAG, "Game loop executed " + tickCount + " times");
 }
}

3. [代碼]MainGamePanel.java
[java]  package net.obviam.droidz; 
 
import android.content.Context; 
import android.graphics.Canvas; 
import android.view.MotionEvent; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 
 
public class MainGamePanel extends SurfaceView implements 
  SurfaceHolder.Callback { 
 
 private MainThread thread; 
 
 public MainGamePanel(Context context) { 
  super(context); 
  getHolder().addCallback(this); 
 
  // create the game loop thread  
  thread = new MainThread(); 
 
  setFocusable(true); 
 } 
 
 @Override 
 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 
 } 
 
 @Override 
 public void surfaceCreated(SurfaceHolder holder) { 
  thread.setRunning(true); 
  thread.start(); 
 } 
 
 @Override 
 public void surfaceDestroyed(SurfaceHolder holder) { 
  boolean retry = true; 
  while (retry) { 
   try { 
    thread.join(); 
    retry = false; 
   } catch (InterruptedException e) { 
    // try again shutting down the thread  
   } 
  } 
 } 
 
 @Override 
 public boolean onTouchEvent(MotionEvent event) { 
  return super.onTouchEvent(event); 
 } 
 
 @Override 
 protected void onDraw(Canvas canvas) { 
 } 

package net.obviam.droidz;

import android.content.Context;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MainGamePanel extends SurfaceView implements
  SurfaceHolder.Callback {

 private MainThread thread;

 public MainGamePanel(Context context) {
  super(context);
  getHolder().addCallback(this);

  // create the game loop thread
  thread = new MainThread();

  setFocusable(true);
 }

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

 @Override
 public void surfaceCreated(SurfaceHolder holder) {
  thread.setRunning(true);
  thread.start();
 }

 @Override
 public void surfaceDestroyed(SurfaceHolder holder) {
  boolean retry = true;
  while (retry) {
   try {
    thread.join();
    retry = false;
   } catch (InterruptedException e) {
    // try again shutting down the thread
   }
  }
 }

 @Override
 public boolean onTouchEvent(MotionEvent event) {
  return super.onTouchEvent(event);
 }

 @Override
 protected void onDraw(Canvas canvas) {
 }
}

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.