当前位置:操作系统 > 安卓/Android >>

一款非常不错的android游戏循环原理

解说一款非常不错的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); 
        // 易做图 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);
        // 易做图 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.Su易做图ceHolder; 
 
public class MainThread extends Thread { 
 
 private static final String TAG = MainThread.class.getSimpleName(); 
 
 private Su易做图ceHolder su易做图ceHolder; 
 private MainGamePanel gamePanel; 
 private boolean running; 
 public void setRunning(boolean running) { 
  this.running = running; 
 } 
 
 public MainThread(Su易做图ceHolder su易做图ceHolder, MainGamePanel gamePanel) { 
  super(); 
  this.su易做图ceHolder = su易做图ceHolder; 
  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.Su易做图ceHolder;

public class MainThread extends Thread {

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

 private Su易做图ceHolder su易做图ceHolder;
 private MainGamePanel gamePanel;
 private boolean running;
 public void setRunning(boolean running) {
  this.running = running;
 }

 public MainThread(Su易做图ceHolder su易做图ceHolder, MainGamePanel gamePanel) {
  super();
  this.su易做图ceHolder = su易做图ceHolder;
  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.Su易做图ceHolder; 
import android.view.Su易做图ceView; 
 
public class MainGamePanel extends Su易做图ceView implements 
  Su易做图ceHolder.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 su易做图ceChanged(Su易做图ceHolder holder, int format, int width, int height) { 
 } 
 
 @Override 
 public void su易做图ceCreated(Su易做图ceHolder holder) { 
  thread.setRunning(true); 
  thread.start(); 
 } 
 
 @Override 
 public void su易做图ceDestroyed(Su易做图ceHolder holder) { 
  boolean retry = true; 
  while (retry) { 
   try { 
    thread.join(); 
    retry = false; 
   } catch (InterruptedException e) { 
    // try again shutting down the thread  
   } 
  } 
 } 
 
 @Override 
&n

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,