📄 j2me 2d小游戏入门之游戏的框架.htm
字号:
<DIV id=xfdh><A class=headblack-s href="http://cseek.yesky.com/"><SPAN
style="COLOR: #f04400">消费导购</SPAN></A></DIV>
<DIV class=headbg3>
<DIV id=span_m3per1_w1><A class=headblack
href="http://cseek.yesky.com/">商情</A></DIV>
<DIV id=span_m3per1_w2><A class=headblack
href="http://mobile.yesky.com/">手机</A></DIV>
<DIV id=span_m3per1_w2><A class=headblack
href="http://digital.yesky.com/">数码:</A></DIV>
<DIV id=span_m3per1_w3><A class=headblack-s href="http://mp3.yesky.com/"><SPAN
class=fontsize>MP3</SPAN>随身听</A></DIV>
<DIV id=span_m3per1_w3><A class=headblack-s
href="http://dc.yesky.com/">数码相机</A></DIV>
<DIV id=span_m3per1_w2><A class=headblack-s
href="http://dv.yesky.com/">摄像机</A></DIV>
<DIV id=span_m3per1_w2><A class=headblack
href="http://notebook.yesky.com/">笔记本</A></DIV>
<DIV id=span_m3per1_w2><A class=headblack
href="http://myhard.yesky.com/">硬件:</A></DIV>
<DIV id=span_m3per1_w2><A class=headblack-s
href="http://myhard.yesky.com/pc/">品牌机</A></DIV>
<DIV id=span_m3per1_w1><A class=headblack-s
href="http://myhard.yesky.com/acceessory/">配件</A></DIV>
<DIV id=span_m3per1_w1><A class=headblack-s
href="http://myhard.yesky.com/oa">外设</A></DIV>
<DIV id=span_m3per1_w1><A class=headblack-s
href="http://myhard.yesky.com/net/">网络</A></DIV>
<DIV id=span_m3per1_w1><A class=headblack-s
href="http://myhard.yesky.com/vga/">显卡</A></DIV>
<DIV id=span_m3per1_w1><A class=headblack-s
href="http://myhard.yesky.com/projector/">投影</A></DIV>
<DIV id=span_m3per1_w2><A class=headblack-s
href="http://myhard.yesky.com/scanner/">扫描仪</A></DIV>
<DIV id=span_m3per1_w1><A class=headblack-s
href="http://myhard.yesky.com/laserjet/">激打</A></DIV>
<DIV id=span_m3per1_w1><A class=headblack-s
href="http://myhard.yesky.com/inkjet/">喷打</A></DIV>
<DIV id=span_m3per1_w3><A class=headblack
href="http://dh.yesky.com/">数字家庭</A></DIV></DIV>
<DIV class=left><IMG src="J2ME 2D小游戏入门之游戏的框架.files/index-nav12.gif"
border=0></DIV></DIV><!--菜单导航结束-->
<DIV class=clear1></DIV><!--笔记本导航条-->
<DIV class=newbox>
<DIV class=newbgpic>
<DIV class=left>您现在的位置:<A href="http://www.yesky.com/"> Yesky</A>><A
href="http://soft.yesky.com/"> 软件</A>><A href="http://dev.yesky.com/">
开发者网络</A> </DIV>
<DIV class=right>
<DIV class=space1><SPAN id=ad3></SPAN></DIV></DIV></DIV></DIV>
<DIV class=clear1></DIV><!--结束-->
<DIV class=clear1></DIV><!--文章内容区-->
<DIV class=newbox>
<DIV class=newleft>
<DIV class=newleft1>
<DIV class=newsmallbox>J2ME 2D小游戏入门之游戏的框架 </DIV>
<DIV class=clear1></DIV>
<DIV class=newsmall1box>
<DIV class=newtitle1>作者: favoyang </DIV>
<DIV class=newtitle2>出处: J2ME开发网 </DIV>
<DIV class=newtitle3>责任编辑: 方舟 </DIV>
<DIV class=newtitle4>[ 2004-11-02 08:49 ]</DIV></DIV>
<DIV class=clear1></DIV>
<DIV class=space2><SPAN id=ad5></SPAN></DIV><BR>
<DIV class=newmiddlebox style="OVERFLOW: hidden">
<DIV class=guanggao><SPAN id=ad4></SPAN></DIV> 一、游戏的框架
<BR><BR> 我们的游戏需要一个通用的游戏框架,也方便以后的开发,但实现一个引擎是复杂的。作为初学者如果要你考虑太多的问题,恐怕会让你偏离主线,这里只给出canvas的代码,不理解可以参看本站的另外一篇系列文章《使用MIDP2.0开发游戏》。<BR><BR>
<TABLE borderColor=#ffcc66 width="90%" align=center bgColor=#e6e4dd border=1>
<TBODY>
<TR>
<TD>public class MyGameCanvas extends GameCanvas<BR> implements Runnable,
CommandListener{<BR> private static MyGameCanvas instance;<BR> Graphics
g;<BR> boolean running;<BR> Thread t;<BR> Command
startcmd,exitcmd,restartcmd;<BR> int keystate;<BR> boolean
keyevent;<BR> boolean
key_up,key_down,key_left,key_right,key_fire;<BR> private boolean
allowinput;<BR> public int screenwidth;<BR> public int
screenheight;<BR> boolean gameover;<BR> //define your variable
here<BR> //define your variable end<BR> protected MyGameCanvas()
{<BR> super(true);<BR> g=getGraphics();<BR> running=false;<BR> t=null;<BR> addCommand(startcmd=new
Command("start",Command.OK,1));<BR> addCommand(exitcmd=new
Command("exit",Command.EXIT,1));<BR> setCommandListener(this);<BR> screenwidth=getWidth();<BR> screenheight=getHeight();<BR> //put
your init once code here<BR> //put your init once code
end<BR> }<BR> synchronized public static MyGameCanvas getInstance()
{<BR> if (instance == null) {<BR> instance = new
MyGameCanvas();<BR> System.out.println("new
MyGameCanvas");<BR> }<BR> return instance;<BR> }<BR> public void
run(){<BR> System.out.println("MyGameCanvas run start");<BR> long
st=0,et=0,diff=0;<BR> int rate=50;//16-17 frame per
second<BR> while(running){<BR> st=System.currentTimeMillis();<BR> gameinput();<BR> gameMain();<BR> et=System.currentTimeMillis();<BR> diff=et-st;<BR> if(diff<rate){<BR> //System.out.println("Sleep
"+(rate-diff));<BR> try {<BR> Thread.sleep(rate -
diff);<BR> }<BR> catch (InterruptedException ex)
{}<BR> }else{<BR> //System.out.println("rush , and the frame using
time: "+diff);<BR> }<BR> }<BR> System.out.println("MyGameCanvas run
end");<BR> }<BR> public void
start(){<BR> if(!running){<BR> running=true;<BR> t=new
Thread(this);<BR> t.start();<BR> }<BR> }<BR> private void
gameMain() {<BR> g.setColor(0,0,0);//clear
screen<BR> g.fillRect(0,0,getWidth(),getHeight());<BR> flushGraphics();<BR> }<BR> private
void gameInit()
{<BR> gameover=false;<BR> allowinput=true;<BR> key_up=key_down=key_left=key_right=key_fire=false;<BR> }<BR> public
void stop(){<BR> if(running){<BR> running =
false;<BR> }<BR> }<BR> public void commandAction(Command c,
Displayable d) {<BR> String
cmdstr=c.getLabel();<BR> if(cmdstr.equals("start")){<BR> gameInit();<BR> start();<BR> removeCommand(startcmd);<BR> addCommand(restartcmd=new
Command("restart",Command.OK,1));<BR> }else
if(cmdstr.equals("restart")){<BR> stop();<BR> while(t.isAlive());<BR> gameInit();<BR> start();<BR> }else
if(cmdstr.equals("exit")){<BR> stop();<BR> Navigate.midlet.destroyApp(false);<BR> Navigate.midlet.notifyDestroyed();<BR> }<BR> }<BR> private
void gameinput()
{<BR> if(allowinput){<BR> keystate=getKeyStates();<BR> keyevent=false;<BR> if((keystate
&
UP_PRESSED)!=0){//up<BR> key_up=true;keyevent=true;<BR> //deal
your unstop job code here<BR> //System.out.println("up
press");<BR> //deal your unstop job code end<BR> }else if((keystate
& UP_PRESSED)==0){//release
key<BR> if(key_up==true){<BR> key_up=false;<BR> //deal your
one press-one job code here<BR> //System.out.println("up
release");<BR> //deal your one press-one job code
end<BR> }<BR> }<BR> if((keystate &
DOWN_PRESSED)!=0){//down<BR> key_down=true;keyevent=true;<BR> //deal
your unstop job code here<BR> //System.out.println("down
press");<BR> //deal your unstop job code end<BR> }else if((keystate
& DOWN_PRESSED)==0){//release
key<BR> if(key_down==true){<BR> key_down=false;<BR> //deal
your one press-one job code here<BR> //System.out.println("down
release");<BR> //deal your one press-one job code
end<BR> }<BR> }<BR> if((keystate &
LEFT_PRESSED)!=0){//left<BR> key_left=true;keyevent=true;<BR> //deal
your unstop job code here<BR> //System.out.println("left
press");<BR> //deal your unstop job code end<BR> }else if((keystate
& LEFT_PRESSED)==0){//release
key<BR> if(key_left==true){<BR> key_left=false;<BR> //deal your
one press-one job code here<BR> //System.out.println("left
release");<BR> //deal your one press-one job code
end<BR> }<BR> }<BR> if((keystate &
RIGHT_PRESSED)!=0){//right<BR> key_right=true;keyevent=true;<BR> //deal
your unstop job code here<BR> //System.out.println("right
press");<BR> //deal your unstop job code end<BR> }else if((keystate
& RIGHT_PRESSED)==0){//release
key<BR> if(key_right==true){<BR> key_right=false;<BR> //deal
your one press-one job code here<BR> //System.out.println("right
release");<BR> //deal your one press-one job code
end<BR> }<BR> }<BR> if((keystate &
FIRE_PRESSED)!=0){//fire<BR> key_fire=true;keyevent=true;<BR> //deal
your unstop job code here<BR> //System.out.println("fire
press");<BR> //deal your unstop job code end<BR> }else if((keystate
& FIRE_PRESSED)==0){//release
key<BR> if(key_fire==true){<BR> key_fire=false;<BR> //deal your
one press-one job code here<BR> //System.out.println("fire
release");<BR> //deal your one press-one job code
end<BR> }<BR> }<BR> if(!keyevent){<BR> //no keyevent
here<BR> //System.out.println("NO KEY press");<BR> //no keyevent
end<BR> }<BR> }<BR>}<BR><BR>public static void
cleanJob(){<BR> instance=null;<BR>}<BR><BR>}</TD></TR></TBODY></TABLE><BR> 使用singlon实现,因为每个gamecanvas都需要很多的内存空间。另外对我们来说,只要改写gameInit(),gameMain(),一次性初始化的代码写在构造函数中。<BR><BR><BR><BR></DIV>
<DIV class=clear1></DIV></DIV><!--结束-->
<DIV class=clear1></DIV>
<DIV class=newleft1>
<DIV class=space2><SPAN id=ad7></SPAN></DIV></DIV><!--文章评论区-->
<SCRIPT language=JavaScript>
<!--
var aid = 1870809;
var channelid = 324;
//-->
</SCRIPT>
<DIV class=newxiangguan>
<DIV class=newxiangguanmenu><A href="http://my.yesky.com/"
target=_blank>天极社区</A>邀请您:<A class=red-s href="http://blog.yesky.com/"
target=_blank>写博客</A> <A class=red-s href="http://photo.yesky.com/"
target=_blank>上传相片</A> <A class=red-s href="http://bbs.yesky.com/"
target=_blank>论坛聊天</A> <A class=red-s href="http://bbs.yesky.com/"
target=_blank>订阅电子杂志</A> <A class=red-s
href="http://www.yeecai.com/mobileant/tjadd/tjload.html"
target=_blank>彩信蚂蚁</A> <A class=red-s
href="http://hot.yesky.com/">推荐网摘</A> <A class=red-s
href="http://training.yesky.com/">IT培训</A> <A class=red-s
href="javascript:sendemail()" target=_self>对此感兴趣</A> <A
title="推荐到天极网摘 http://hot.yesky.com [鼠标划选本文摘要,然后点击本图片]"
href="javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(yesky=window.open('http://hot.yesky.com/dp.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t)+'&st=2','yesky','scrollbars=no,width=400,height=480,left=75,top=20,status=no,resizable=yes'));yesky.focus();"
target=_self><IMG src="J2ME 2D小游戏入门之游戏的框架.files/souchang1.gif" border=0></A>
</DIV>
<DIV class=newxiangguancontent><SPAN style="FONT-SIZE: 9pt"><B>收藏到:</B><A
title=天极网摘
href="javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(yesky=window.open('http://hot.yesky.com/dp.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t)+'&st=2','yesky','scrollbars=no,width=400,height=480,left=75,top=20,status=no,resizable=yes'));yesky.focus();"
target=_self><SPAN style="COLOR: red">天极网摘</SPAN></A> <A
href="javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(vivi=window.open('http://vivi.sina.com.cn/collect/icollect.php?pid=wellknow&title='+escape(d.title)+'&url='+escape(d.location.href)+'&desc='+escape(t),'vivi','scrollbars=no,width=480,height=480,left=75,top=20,status=no,resizable=yes'));vivi.focus();"
target=_self><SPAN style="COLOR: red">新浪VIVI</SPAN></A> <A title=bangchen.com出品
href="javascript:t=document.title;u=location.href;e=document.selection?(document.selection.type!='None'?document.selection.createRange().text:''):(document.getSelection?document.getSelection():'');void(open('http://bookmark.hexun.com/post.aspx?title='+escape(t)+'&url='+escape(u)+'&excerpt='+escape(e),'HexunBookmark','scrollbars=no,width=600,height=450,left=80,top=80,status=no,resizable=yes'));"
target=_self><SPAN style="COLOR: red">和讯网摘</SPAN></A> <A title=博彩中心
href="javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(boit=window.open('http://blogmark.blogchina.com/jsp/key/quickaddkey.jsp?k='+encodeURI(d.title)+'&u='+encodeURI(d.location.href)+'&c='+encodeURI(t),'boit','scrollbars=no,width=500,height=430,status=no,resizable=yes'));boit.focus();"
target=_self><SPAN style="COLOR: red">博彩中心</SPAN></A> <A
href="javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();"
target=_self><SPAN style="COLOR: red">365Key网摘</SPAN></A> <A title=poco网摘
href="javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://my.poco.cn/fav/storeIt.php?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();"
target=_self><SPAN style="COLOR: red">poco网摘</SPAN></A> <A title=加入狐摘
href="javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://z.sohu.com/storeit.do?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();"
target=_self><SPAN style="COLOR: red">狐摘</SPAN></A> <A
href="javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(websnip=window.open('http://x.yeeyoo.com/MouseAdd.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'yeeyoo','scrollbars=no,width=475,height=450,left=280,top=50,status=no,resizable=yes'));websnip.focus();;"
target=_self><SPAN style="COLOR: red">亿友响享Yeeyoo</SPAN></A> <BR><A
href="javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.wzChina.net/collect2.asp?title='+escape(d.title)+'&u='+escape(d.location.href)+'&brief='+escape(t),'keyit','scrollbars=no,width=500,height=520,left=75,top=10,status=no,resizable=yes'));"
target=_self><FONT colr="#222222">网摘中国</FONT></A> <A title=加加文摘
href="javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(jiait=window.open('http://z365.cn/saveZ.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'jiait','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));jiait.focus();"
target=_self>加加文摘</A> <A
href="javascript:u=location.href;t=document.title;void(open('http://www.igooi.com/addnewitem.aspx?noui=yes&jump=close&url='+escape(u)+'&title='+escape(t)+'&sel='+escape(document.selection.createRange().text),'igooi', 'toolbar=no,width=400,height=480'));"
target=_self>igooi-it网摘</A> <A title=5seek网摘
href="javascript:d=document;if%20(!sel)%20{var%20sel%20=%20'';};char%20=d.charset;title=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.5seek.com/storeit.asp?title='+escape(d.title)+'&url='+escape(d.location.href)+'&char='+escape(d.char)+'&sel='+escape(d.sel),'keyit','scrollbars=no,width=515,height=555,left=75,top=20,status=yes,resizable=yes'));keyit.focus();"
target=_self>5seek网摘</A> <A title=36963网摘
href="javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.i2key.com/StoreIt.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=475,left=75,top=20,status=no,resizable=yes'));keyit.focus();"
target=_self>I2Key</A> <A title=帮衬国际出品
href="javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(wozhai=window.open('http://www.wozhai.com/wozhai/Cento.asp#t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'wozhai','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));wozhai.focus();"
target=_self>我摘网摘</A> <A title=天下图摘(www.cn3.cn)
href="javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.cn3.cn/user/addurl.asp?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=490,height=450,left=120,top=50,status=no,resizable=yes'));keyit.focus();"
target=_self><FONT color=#cc0000>天下图摘</FONT></A> <A title=YouNote
href="javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(youit=window.open('http://www.younote.com/Noteit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'youit','scrollbars=no,width=475,height=575,status=no,resizable=yes'));youit.focus();"
target=_self>YouNote</A> <A title=百特门
href="javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange ().text:''):(d.getSelection?d.getSelection():'');void(vkey=window.open('http://www.bytemen.net/vkey/AddText.aspx?site=2005&t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'vkey','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));vkey.focus();"
target=_self>百特门</A> </SPAN><BR></DIV></DIV>
<DIV class=clear1></DIV>
<DIV class=pinlunbox>
<DIV class=pinlunguan3><SPAN id=commentsSpanText>
<SCRIPT language=JavaScript><!--function getTagId(tName) { tName = tName.replace(/(^[\s ]*)|([\s ]*$)/g, ""); var tagid = 0; for(var ti = 0; ti < tName.length; ti++) { tagid += tName.charCodeAt(ti); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -