欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现完整游戏循环的方法

程序员文章站 2023-12-01 19:44:40
本文实例讲述了android实现完整游戏循环的方法。分享给大家供大家参考。具体如下: 1. droidzactivity.java: package net.o...

本文实例讲述了android实现完整游戏循环的方法。分享给大家供大家参考。具体如下:

1. droidzactivity.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();
 }
}

2. mainthread.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");
 }
}

3. maingamepanel.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) {
 }
}

希望本文所述对大家的android程序设计有所帮助。