⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 j2me手机游戏引擎程序结构简述-开发者网络-j2me-天极yesky.htm

📁 j2me的一些东西。有用的东东
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0038)http://dev.yesky.com/221/2394221.shtml -->
<HTML lang=zh-CN xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>J2ME手机游戏引擎程序结构简述-开发者网络-J2ME-天极Yesky</TITLE>
<META content=J2ME手机游戏引擎程序结构简述, name=description>
<META content=J2ME手机游戏引擎程序结构简述, name=keywords>
<META content="天极Yesky | 全球中文IT第一门户" name=author>
<META content="天极Yesky | 全球中文IT第一门户" name=Copyright>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META http-equiv=Content-Language content=zh-CN><LINK media=all 
href="J2ME手机游戏引擎程序结构简述-开发者网络-J2ME-天极Yesky.files/y_mobile_content.css" 
type=text/css rel=stylesheet>
<SCRIPT src="J2ME手机游戏引擎程序结构简述-开发者网络-J2ME-天极Yesky.files/showlay.js"></SCRIPT>

<META content="MSHTML 6.00.2900.3132" name=GENERATOR></HEAD>
<BODY><!--页面头部--><!--头部-->
<DIV id=topmenu><A href="http://www.yesky.com/">Yesky首页</A>| <A 
href="http://product.yesky.com/">产品报价</A>| <A 
href="http://cseek.yesky.com/">行情</A>| <A href="http://mobile.yesky.com/">手机</A> 
| <A href="http://digital.yesky.com/">数码</A> | <A 
href="http://notebook.yesky.com/">笔记本</A> | <A 
href="http://pc.yesky.com/">台式机</A> | <A href="http://diy.yesky.com/">DIY硬件</A> 
| <A href="http://oa.yesky.com/">外设</A> | <A href="http://net.yesky.com/">网络</A> 
| <A href="http://dh.yesky.com/">数字家庭</A> | <A 
href="http://lab.yesky.com/">评测</A> | <A href="http://soft.yesky.com/">软件</A> | 
<A href="http://e.yesky.com/">e时代</A> | <A href="http://game.yesky.com/">游戏</A> 
| <A href="http://pic.yesky.com/">图片</A> | <A 
href="http://desktop.yesky.com/">壁纸</A> | <A href="http://q.yesky.com/">群乐</A> | 
<A href="http://my.yesky.com/">社区</A> | <A href="http://blog.yesky.com/">博客</A> 
| <A href="http://www.mydown.com/">下载</A></DIV>
<DIV id=topbox>
<DIV class=tleft><A href="http://www.yesky.com/"><IMG alt=天极Yesky_全球中文IT第一门户 
src="J2ME手机游戏引擎程序结构简述-开发者网络-J2ME-天极Yesky.files/logo.gif" border=0></A></DIV>
<DIV class=tad><SPAN id=ad1></SPAN></DIV>
<DIV id=search>
<DIV>
<FORM name=searchform action=http://search.chinabyte.com/search method=get 
target=_blank><INPUT id=searchzz onmouseover=this.focus() onfocus=this.select() 
name=q><INPUT type=hidden value=GB2312 name=encoding><INPUT type=hidden 
value=connect name=dir><INPUT type=hidden value=20002 name=cid><INPUT id=search_btn type=submit value=天极搜索> </FORM></DIV>
<DIV class=tdq>
<DIV class=rightarea><A href="http://bj.yesky.com/">北京</A> <A 
href="http://hd.yesky.com/">上海</A> <A href="http://gd.yesky.com/">广东</A> <A 
href="http://xa.yesky.com/">西安</A><BR><A href="http://cq.yesky.com/">重庆</A> <A 
href="http://cd.yesky.com/">成都</A> <A href="http://sy.yesky.com/">沈阳</A> <A 
href="http://js.yesky.com/">江苏</A> </DIV>
<DIV class=rightarea_right><A 
href="http://my.yesky.com/">用户<BR>登录</A></DIV></DIV></DIV>
<DIV class=tright>
<DIV class=box1>
<SCRIPT src="J2ME手机游戏引擎程序结构简述-开发者网络-J2ME-天极Yesky.files/content_text_ad.js" 
type=text/javascript></SCRIPT>
</DIV></DIV></DIV>
<SCRIPT src="J2ME手机游戏引擎程序结构简述-开发者网络-J2ME-天极Yesky.files/flash.js" 
type=text/javascript></SCRIPT>

<DIV id=childmenu><A href="http://soft.yesky.com/">软件频道&gt;</A><A 
href="http://dev.yesky.com/">程序开发&gt;</A><A 
href="http://dev.yesky.com/devjava/">Java</A><A 
href="http://dev.yesky.com/ysdevvb">VB</A><A 
href="http://dev.yesky.com/ysdevvc">VC</A><A 
href="http://dev.yesky.com/ysdevdelphi">Delphi</A><A 
href="http://dev.yesky.com/ysdevc">C/C++</A><A 
href="http://dev.yesky.com/web/">Web开发</A><A 
href="http://dev.yesky.com/msdn/">微软专栏</A><A 
href="http://dev.yesky.com/devydkf/">移动</A><A 
href="http://dev.yesky.com/devsjk/">数据库</A><A 
href="http://dev.yesky.com/ysdevcxrs">程序人生</A><A 
href="http://dev.yesky.com/ysdevrjgc">软件工程</A>|<A 
href="http://soft.yesky.com/lesson/">产品中心</A><A 
href="http://www.mydown.com/soft/develop/news.shtml">下载</A><A 
href="http://q.yesky.com/group/179"><FONT 
color=#ff0000>群乐:大家耍QQ</FONT></A></DIV><!--导航条-->
<DIV id=location>
<DIV class=loleft>您现在的位置: <A href="http://www.yesky.com/">天极Yesky</A> &gt; <A 
href="http://soft.yesky.com/">软件频道</A> &gt; <A 
href="http://dev.yesky.com/">开发者网络</A> &gt; J2ME手机游戏引擎程序结构简述</DIV>
<DIV class=adright><SPAN id=ad2></SPAN></DIV></DIV><!--内容块开始-->
<DIV id=conbox><!--左边开始-->
<DIV id=conleft>
<DIV id=contopla1>
<DIV id=__001>
<DIV class=qkong></DIV>
<DIV class=qw1>全文</DIV>
<DIV class=zkong>
<DIV class=kl></DIV>
<DIV class=bj><A href="http://dev.yesky.com/221/2394221.shtml#pls" 
target=_self>评论</A></DIV>
<DIV class=kr></DIV>
<SCRIPT type=text/javascript>
var titiepic ="#";
if (titiepic!="#")
{
document.write("<div class=\"kl\"></div><div class=\"bj\"><a href=\"#\" target=\"_blank\">图片</a></div><div class=\"kr\"></div>");
}
</SCRIPT>
</DIV></DIV></DIV>
<DIV class=topadbg><SPAN id=ad10></SPAN></DIV><A name=top></A>
<DIV id=contitle>
<H1>J2ME手机游戏引擎程序结构简述</H1></DIV>
<DIV id=conauthor><SPAN>2006-05-11 08:13 </SPAN><SPAN>作者: </SPAN><SPAN>出处: 
j2me开发网 </SPAN><SPAN>责任编辑:<A title=向本编辑提问 
href="http://comments.yesky.com/t/·&frac12;&Ouml;&Ucirc;/6,324/2394221.shtml" target=_blank>方舟</A> 
</SPAN></DIV>
<DIV class=topadbg><SPAN id=ad9></SPAN></DIV>
<DIV id=conneirong><SPAN id=ad3></SPAN>
<DIV class=guanggao><SPAN 
id=contentAdv></SPAN></DIV>  游戏引擎的结构很多,不过基本上都是在一个游戏主循环内实现。程序里面的主循环包含了程序框架的最主要的结构体。J2me的程序一般都包含两个class文件,一个是MIDlet,一个是Displayable。一般我都是把游戏的主要代码放在Displayable这个类里面。这个类是基于事件驱动的程序,有三个主要相应函数void 
paint(Graphics g),void keyPressed(int keyCode),void keyReleased(int keyCode)。 
<BR><BR>  1.使用Runnable和创建线程的主循环<BR><BR>  一般主体的做法就是让Displayable这个类实现Runnable这个接口,然后在其构造函数中创建一个线程,启动其run()函数,而run函数里面就包含了游戏的主循环。下面是我在仙剑里面的片断代码。<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
  <TBODY>
  <TR>
    <TD>public class GameMIDlet extends MIDlet {<BR> static GameMIDlet 
      instance;<BR> Display display;<BR> GameDisplayable displayable = 
      null;<BR> public GameMIDlet() {<BR>  instance = this;<BR>  display = 
      Display.getDisplay(this);<BR>  displayable = new 
      GameDisplayable();<BR> }<BR><BR> public void startApp() 
      {<BR>  display.setCurrent(displayable);<BR> }<BR><BR> public void 
      pauseApp() {}<BR><BR> public void destroyApp(boolean unconditional) 
      {<BR>  displayable.running = false;<BR> }<BR><BR> public static void 
      quitApp() 
      {<BR>  instance.destroyApp(true);<BR>  instance.notifyDestroyed();<BR>  instance 
      = null;<BR> }<BR>}<BR><BR>public class GameDisplayable extends FullCanvas 
      implements Runnable {<BR> /** 主控制线程 */<BR> Thread MainThread = 
      null;<BR> /** 游戏时钟间隔 毫秒为单位 */<BR> public static long timeinterval = 
      20;<BR> public static boolean Isstable = true;<BR> /* 用于游戏时钟的变量 
      */<BR> public static long timeold = 0;<BR> public static long timenow = 
      0;<BR> public long interval = 0;<BR> public static long frames_per_second 
      = 0;<BR> int count = 0;<BR> long second = 0;<BR> public static boolean 
      running = true;<BR> public GameDisplayable() {<BR>  // 开始主线程<BR>  Thread 
      MainThread = new Thread(this);<BR>  MainThread.start();<BR> }<BR> public 
      void run() {<BR>  while (running) {<BR>   timenow = 
      System.currentTimeMillis();<BR>   interval = timenow - timeold;<BR>   if 
      (interval &gt;= timeinterval) {<BR>    timeold = 
      timenow;<BR>    Game_Process();<BR>    if (second != 
      (System.currentTimeMillis() / 1000)) {<BR>     second = 
      System.currentTimeMillis() / 1000;<BR>     frames_per_second = 
      count;<BR>     count = 
      1;<BR>    }<BR>    else<BR>     count++;<BR>   }<BR>   lib.sleep(30);<BR>  }<BR> } 
    </TD></TR></TBODY></TABLE><BR>  其中关于控制主循环速度的代码可以不要,但是lib.sleep(30)必须保留,因为在Nokia 
60的手机上,如果去除了sleep(30),那么游戏将无法切换回来。同时,在游戏中任何一个内部循环中,也必须加入sleep(30)这个等待,才能让游戏可以切换回来,至于为什么这样做,我暂时还不清楚。30ms是我测试过没有问题的数值,可能比30ms还小的值也是没有问题的。<BR><BR>  同时,在MOTO的手机上,必须将游戏的主循环放在一个线程中,游戏才能切换回来,不过可以不加上面说的sleep(30)延时。<BR><BR>  2.不使用线程的主循环办法<BR><BR>  这个办法只能在Nokia的平台上实现,而我只建议在Nokia 
40的平台上做,这样不需要线程,道理上来说节约了一些内存,如果不是内存很紧张的机型,那么最好还是使用上一种办法。<BR><BR>  游戏的主循环放在MIDlet的class里面,具体做法如下:<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
  <TBODY>
  <TR>
    <TD>public class GameMIDlet extends MIDlet {<BR> GameDisplayable 
      displayable = null;<BR> /** 游戏时钟间隔 毫秒为单位 */<BR> public static long 
      timeinterval = 0;<BR> //用于游戏时钟的变量<BR> public static long timeold = 
      0;<BR> public static long timenow = 0;<BR> public long interval = 
      0;<BR> public static long frames_per_second=0;<BR> int count=0;<BR> long 
      second =0;<BR> public static boolean running = false;<BR> static boolean 
      exitApp =false;<BR> public GameMIDlet() {<BR>  displayable = new 
      GameDisplayable();<BR>  running =true;<BR> }<BR><BR> public void 
      startApp() {<BR>  running 
      =true;<BR>  Display.getDisplay(this).setCurrent(displayable);<BR>  while(running) 
      {<BR>   timenow = System.currentTimeMillis();<BR>   interval = timenow - 
      timeold;<BR>   if (interval &gt;= timeinterval) {<BR>    timeold = 
      timenow;<BR>    displayable.Game_Process();<BR>    if(second != 
      (System.currentTimeMillis() /1000)){<BR>     second = 
      System.currentTimeMillis()/1000;<BR>     frames_per_second = 
      count;<BR>     count = 1;<BR>    }else<BR>     count 
      ++;<BR>   }<BR>  }<BR>  if(exitApp) 
      {<BR>   destroyApp(true);<BR>   notifyDestroyed();<BR>  }<BR> }<BR><BR> public 
      void pauseApp() {<BR>  running =false;<BR> }<BR><BR> public void 
      destroyApp(boolean unconditional) {<BR>  running = 
      false;<BR> }<BR><BR> public static void quitApp() {<BR>  running 
      =false;<BR>  exitApp =true;<BR> }<BR><BR>} </TD></TR></TBODY></TABLE></DIV><!--广告代码开始-->
<SCRIPT>
var ad_cid;
if (window.location.search.substring(1) != "")
{
ad_cid = window.location.search.substring(1);
} else {
ad_cid = 412;
}
</SCRIPT>
<!--小通栏:c-scolumn顶部500*90--><SPAN id=span_ad1>
<SCRIPT language=JavaScript>
document.write("<script src=\"http://vd.yesky.com/html/"+ad_cid+"/"+ad_cid+"_c-scolumn.js\"><\/script>");</SCRIPT>
</SPAN>
<SCRIPT language=JavaScript>
document.getElementById("ad1").innerHTML=document.getElementById("span_ad1").innerHTML;
document.getElementById("span_ad1").innerHTML="";
</SCRIPT>
<!--c-pip文章正文中间240*200--><SPAN id=span_ad3>
<SCRIPT language=JavaScript>
document.write("<script src=\"http://vd.yesky.com/html/"+ad_cid+"/"+ad_cid+"_c-pip.js\"><\/script>");</SCRIPT>
</SPAN>
<SCRIPT>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -