📄 j2me 2d小游戏入门之实现爆炸.htm
字号:
<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-08 15:22 ]</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> 很多人认为游戏的效果层有时和跟游戏逻辑本身并没有太大的关系,往往就是在最终屏幕上再画上一层效果层。但是游戏逻辑和效果层之间的通信是很重要的。这种通信往往体现在延时与等待上。比如飞机爆炸时,不接受任何用户输入,并且爆炸效果还要继续跟随飞机坠落,甚至爆炸的范围会影响周围的物体,要等待爆炸结果结束了才继续进行游戏。游戏逻辑和效果层之间的通信是很复杂的问题。在这里我突然有了罪恶感,我们没有对游戏进行任何的分析就起步了,游戏完全是基于硬编码的,我想到那儿,大家跟着看到那儿。飞机类仅仅是一个sprite,没有设计成一个状态机,这也就使得我们的效果层和逻辑层的通信有些卡通了。也许本文给了你编写自己第一个游戏的喜悦,也带给了你对游戏扩展性与复杂性的一丝担忧。或许这比便一个硬编码的游戏更有意义呢?谁说得好呢,现还是以为那些扩展性良好的游戏是伟大游戏构架师的杰作吧,相信你有了一两个好的想法后会重新设计这个游戏的,使之稍微有一些像个“系统”。然而好的技术不一定产生好的游戏。<BR><BR> 有扯远了,会到现实吧,boys
and
girls!goon.<BR><BR> 描述一下我们的爆炸效果,在子弹击中飞机后,子弹要迅速消失,飞机图像保持不变,此时将爆炸效果至于飞机图像之上,然后开始显示boom动画,在此期间,飞机不接受任何移动指示,因为他lose
control。在爆炸效果后飞机消失。<BR><BR>
<TABLE width="90%" align=center border=0>
<TBODY>
<TR>
<TD>
<DIV align=center><IMG
onerror="this.src='http://www.yesky.com/image20010518/195557.png';"
hspace=3 src="J2ME 2D小游戏入门之实现爆炸.files/195557.png" align=center vspace=1
border=1></DIV></TD></TR></TBODY></TABLE><BR> 我们的爆炸效果类:<BR><BR>
<TABLE borderColor=#ffcc66 width="90%" align=center bgColor=#e6e4dd border=1>
<TBODY>
<TR>
<TD>GameObject explosion;// </TD></TR></TBODY></TABLE><BR> 初始化once:<BR><BR>
<TABLE borderColor=#ffcc66 width="90%" align=center bgColor=#e6e4dd border=1>
<TBODY>
<TR>
<TD>img=ImageTools.getImage("/pic/explosion.png");// <BR>explosion=new
GameObject(img,32,32);</TD></TR></TBODY></TABLE><BR> 初始化:<BR><BR>
<TABLE borderColor=#ffcc66 width="90%" align=center bgColor=#e6e4dd border=1>
<TBODY>
<TR>
<TD>explosion.reset();<BR>explosion.lifetime=3;//生命周期定位三桢</TD></TR></TBODY></TABLE><BR> 逻辑处理:<BR><BR>
<TABLE borderColor=#ffcc66 width="90%" align=center bgColor=#e6e4dd border=1>
<TBODY>
<TR>
<TD>if (gameover)
{//如果游戏结束,显示效果类<BR> explosion.paint(g);<BR> explosion.update();<BR> if(!explosion.alive){//当生命周期结束了<BR> plane.alive=false;//关闭plane<BR> g.setColor(255,255,255);//输出评语,下回分解<BR> g.drawString(StringTools.timeOpinion(gametime),5,22,g.LEFT|g.TOP);<BR> g.drawString("fly
0.1 ver by favo yang",2,100,g.LEFT|g.TOP);<BR> g.drawString("E-mail :
favoyang@yahoo.com",2,115,g.LEFT|g.TOP);<BR> g.drawString("simulate
from:",2,130,g.LEFT|g.TOP);<BR> g.drawString("Mr.tony 's <hold on
20sec 1.20> ",2,145,g.LEFT|g.TOP);<BR> g.drawString("hello tony, just
funny.",2,160,g.LEFT|g.TOP);<BR> }<BR>}</TD></TR></TBODY></TABLE><BR> 现在你看我是如何解决效果层与逻辑层之间的通信的,我使用的是全局变量gameover,在简单游戏中使用大量的全局状态变量也是一种常见的方法,可以避免动脑劲。不过缺点明显,游戏硬编码,结构既不清晰也不漂亮,几乎没有扩展性。所以说最好还是将飞机基于状态机设计,并将效果类设计成含有回调函数的抽象类,然后继承效果类实现回调函数来实现通信。至于总体层次上可以用堆栈将绘画单元串起来。还有分层处理等等…给你个思考的起点…<BR><BR> 导弹的是实现,是不是你已经有个想法了呢,其实就是利用Bullets.killbullets。<BR><BR>
<TABLE width="90%" align=center border=0>
<TBODY>
<TR>
<TD>
<DIV align=center><IMG
onerror="this.src='http://www.yesky.com/image20010518/195558.png';"
hspace=3 src="J2ME 2D小游戏入门之实现爆炸.files/195558.png" align=center vspace=1
border=1></DIV></TD></TR></TBODY></TABLE><BR> 逻辑处理<BR><BR>
<TABLE borderColor=#ffcc66 width="90%" align=center bgColor=#e6e4dd border=1>
<TBODY>
<TR>
<TD>if(bomb.alive){<BR> bomb.moveto(plane.sprite.getX()-20,plane.sprite.getY()-20);<BR> bomb.paint(g);<BR> bomb.update();<BR> bullets.killbullets(plane.sprite,32);<BR>}</TD></TR></TBODY></TABLE><BR> 在这里我不得不提一句,将生命概念封装在GameObject中是很好的(其实我们只是将其用作显示关键字),但将生命周期安排在GameObject中有欠妥当,生命周期也不一定就是基于桢的,有时基于时间,有时还有别的什么。我是说她足够复杂到交给另一个独立类处理,在这里实际需要的是一个足够强大的显示方法,其支持以桢数为参数显示罢了。<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 = 1873127;
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); } return tagid;}function UrlEncode(str){ var i,temp,p,q; var result=""; str = str.replace(/(^[\s ]*)|([\s ]*$)/g, ""); for(i=0;i<str.length;i++){ temp = str.charCodeAt(i); if(temp>=0x4e00){ execScript("ascCode=hex(asc(\""+str.charAt(i)+"\"))", "vbscript"); result+=ascCode.replace(/(.{2})/g, "%$1"); }else{ result+=escape(str.charAt(i)); } } return result;}if(typeof(channelid) == "undefined") channelid = 0;if(typeof(aid) != "undefined") { if(typeof(tagname) == "string" && tagname.length > 0) { document.write(" <a href=\"http://comments.yesky.com/t/"+UrlEncode(tagname)+"/6," + channelid + "/" + aid + ".shtml\" class=\"red-s\" target=\"_blank\">查看主题:<font color=\"#FF0000\">" + tagname + "</font>的内容</a>"); } else { document.write(" <a href=\"http://comments.yesky.com/a/6," + channelid + "/" + aid + ".shtml\" class=\"red-s\" target=\"_blank\">查看所有内容</a>"); }}//--></SCRIPT>
</SPAN>
<SCRIPT language=JavaScript>function showCommentsSpan() { if(typeof(latest_comments_poster) == "object" && latest_comments_poster.length && latest_comments_poster.length > 0) { var re = /(\[s(\d{1,2})\])/ig; var str = "<div class=\"wzplcontent\">"; str += "本文最新主题 共(<span class=\"fontcolor\">"+ latest_comments_num +"</span>)条"; if(typeof(tagname) == "string" && tagname.length > 0) { str += " >> >> >> <a href=\"http://comments.yesky.com/t/" + UrlEncode(tagname) + "/6," + channelid + "/" + aid + ".shtml\" target=\"_blank\" class=\"red-s\"><b>查看主题:</b><font color=\"#FF0000\">" + tagname + "</font>所有主题</a> << << << "; } else { str += " >> >> >> <a href=\"http://comments.yesky.com/a/6," + channelid + "/" + aid + ".shtml\" target=\"_blank\" class=\"red-s\">查看该文章所有相关主题</a> << << << "; } str += "</div>"; var hasVisits = typeof(latest_comments_visits) == "object" && latest_comments_visits.length > 0; var hasTag = typeof(latest_comments_tagid) == "number" && latest_comments_tagid > 0; if(hasVisits) { str += "<table width=\"99%\" border=\"1\" cellspacing=\"0\" cellpadding=\"2\" align=\"center\" style=\"line-height: 120%\" bordercolor=\"#FFFFFF\" bordercolorlight=\"#D0CCD0\">" +"\n"+ " <tr bgcolor=\"#FFFFFF\">" +"\n"+ " <td width=\"7%\">点击</td>" +"\n"+ " <td width=\"7%\">回贴</td>" +"\n"+ " <td width=\"50%\">标题</td>" +"\n"+ " <td width=\"15%\">发表时间</td>" +"\n"+ " <td width=\"21%\">发表人</td>" +"\n"+ "</tr>" ; for (i = 0; i < latest_comments_poster.length && i < 10; i++) { str += "<tr bgcolor=\"#F6F6F6\">" +"\n"+ " <td>" + latest_comments_visits[i] + "</td>" +"\n"+ " <td>" + latest_comments_replies[i] + "</td>"; var url; if (hasTag) { url = "http://comments.yesky.com/l/" + latest_comments_id[i] + "/" + latest_comments_tagid + "/" + latest_comments_type[i] + "," + channelid + "/" + latest_comments_rfid[i] + ".shtml"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -