上一篇:用J2ME开发企业级无线应用 >>
J2ME手机游戏引擎程序结构简述
1.使用Runnable和创建线程的主循环
一般主体的做法就是让Displayable这个类实现Runnable这个接口,然后在其构造函数中创建一个线程,启动其run()函数,而run函数里面就包含了游戏的主循环。下面是我在仙剑里面的片断代码。
| public class GameMIDlet extends MIDlet { static GameMIDlet instance; Display display; GameDisplayable displayable = null; public GameMIDlet() { instance = this; display = Display.getDisplay(this); displayable = new GameDisplayable(); } public void startApp() { display.setCurrent(displayable); } public void pauseApp() {} public void destroyApp(boolean unconditional) { displayable.running = false; } public static void quitApp() { instance.destroyApp(true); instance.notifyDestroyed(); instance = null; } } public class GameDisplayable extends FullCanvas implements Runnable { /** 主控制线程 */ Thread MainThread = null; /** 游戏时钟间隔 毫秒为单位 */ public static long timeinterval = 20; public static boolean Isstable = true; /* 用于游戏时钟的变量 */ public static long timeold = 0; public static long timenow = 0; public long interval = 0; public static long frames_per_second = 0; int count = 0; long second = 0; public static boolean running = true; public GameDisplayable() { // 开始主线程 Thread MainThread = new Thread(this); MainThread.start(); } public void run() { while (running) { timenow = System.currentTimeMillis(); interval = timenow - timeold; if (interval >= timeinterval) { timeold = timenow; Game_Process(); if (second != (System.currentTimeMillis() / 1000)) { second = System.currentTimeMillis() / 1000; frames_per_second = count; count = 1; } else count++; } lib.sleep(30); } } |
其中关于控制主循环速度的代码可以不要,但是lib.sleep(30)必须保留,因为在Nokia 60的手机上,如果去除了sleep(30),那么游戏将无法切换回来。同时,在游戏中任何一个内部循环中,也必须加入sleep(30)这个等待,才能让游戏可以切换回来,至于为什么这样做,我暂时还不清楚。30ms是我测试过没有问题的数值,可能比30ms还小的值也是没有问题的。
同时,在MOTO的手机上,必须将游戏的主循环放在一个线程中,游戏才能切换回来,不过可以不加上面说的sleep(30)延时。
2.不使用线程的主循环办法
这个办法只能在Nokia的平台上实现,而我只建议在Nokia 40的平台上做,这样不需要线程,道理上来说节约了一些内存,如果不是内存很紧张的机型,那么最好还是使用上一种办法。
游戏的主循环放在MIDlet的class里面,具体做法如下:
| public class GameMIDlet extends MIDlet { GameDisplayable displayable = null; /** 游戏时钟间隔 毫秒为单位 */ public static long timeinterval = 0; //用于游戏时钟的变量 public static long timeold = 0; public static long timenow = 0; public long interval = 0; public static long frames_per_second=0; int count=0; long second =0; public static boolean running = false; static boolean exitApp =false; public GameMIDlet() { displayable = new GameDisplayable(); running =true; } public void startApp() { running =true; Display.getDisplay(this).setCurrent(displayable); while(running) { timenow = System.currentTimeMillis(); interval = timenow - timeold; if (interval >= timeinterval) { timeold = timenow; displayable.Game_Process(); if(second != (System.currentTimeMillis() /1000)){ second = System.currentTimeMillis()/1000; frames_per_second = count; count = 1; }else count ++; } } if(exitApp) { destroyApp(true); notifyDestroyed(); } } public void pauseApp() { running =false; } public void destroyApp(boolean unconditional) { running = false; } public static void quitApp() { running =false; exitApp =true; } } |
下一篇:J2ME游戏开发中的地图设计与绘制 >>
相关文章:
- · 让自己编写的DLL加载到Slot1
- · Platform Builder实践之两个要点
- · Platform Builder实践之文件系统
- · 轻松垮入移动应用程序开发的大门
- · Platform Builder实践之拾遗补缺
- · Platform Builder实践之配置文件
- · Platform Builder实践之编译
- · Platform Builder实践之运行环境
- · DirectShow视频捕捉应用研究
- · EVC++开发实例之浏览器窗口
- · Palm编程实现GPS数据读取
- · eMbedded Visual C++开发入门
- · 基于Nokia S60的游戏开发之五
- · 基于Nokia S60的游戏开发之四
- · 在Pocket PC上编写游戏之九
- · 基于Nokia S60的游戏开发之三
- · 在Pocket PC上编写游戏之八
- · 在Pocket PC上编写游戏之六
- · 在Pocket PC上编写游戏之五
- · 在Pocket PC上编写游戏之三
- · 在Pocket PC上编写游戏之一
- · 为Nokia S60编写移动游戏之二
- · 基于Nokia S60的游戏开发之一
- · 无线开发教程之二
- · 移动程序中使用DataGrid控件
- · Palm OS应用程序设计指南之十
- · Palm OS应用程序设计指南之九
- · Palm OS应用程序设计指南之二
- · EVB3.0中的CommonDialog控件
- · 移动开发2003年综述之应用
- · 移动开发2003年综述之展望
- · 移动开发2003综述之操作系统
- · 移动开发2003综述之技术构架
- · Palm OS基础入门
- · 数据库技术在BREW中的应用
- · windows nt 4.0中文版的开机过程
- · Windows95下多线程编程技术及其实现
- · 浅析Win2000进程的创建
