📄 j2me 2d小游戏入门之周边工具类.htm
字号:
<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-03 09:31 ]</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> 二、完善周边工具类(图象、GameObject、Font)
<BR><BR> 虽然我们有了midp2.0的支持,但是有时还是需要一些辅助工具,方便我们使用。这怕是在进行真正的游戏设计之前最有趣的了。<BR><BR> 1、首先是一个ImageTools工具类,提供一个方法帮助调用Image<BR><BR>
<TABLE borderColor=#ffcc66 width="90%" align=center bgColor=#e6e4dd border=1>
<TBODY>
<TR>
<TD>public class ImageTools {<BR> protected ImageTools() {}<BR><BR> public
static Image getImage(String str){<BR> Image img=null;<BR> try
{<BR> img = Image.createImage(str);<BR> }<BR> catch (Exception ex)
{<BR> System.out.println(ex);<BR> }<BR> finally{<BR> return
img;<BR> }<BR> }<BR>}</TD></TR></TBODY></TABLE><BR> 2.GameObject,提供一个通用的游戏对象。<BR><BR> 有了Sprite类,为什么还要GameObject呢?其实我们一般是将Sprite,看作成一个高级的Image,往往一个Sprite要被多个游戏对象调用,GameObject其实就是Sprite的状态类。GameObject提供简单的生命周期概念,动画更新速度;<BR><BR>
<TABLE borderColor=#ffcc66 width="90%" align=center bgColor=#e6e4dd border=1>
<TBODY>
<TR>
<TD>public class GameObject {<BR> public Sprite
sprite;//内置的Sprite<BR> public boolean alive;//存活标记<BR> private int
lifecount=0;//生命周期计数器<BR> public int lifetime=0;//生命周期,以桢为单位<BR> public
int speed=0;//动画桢更新速度,(0至无穷,0代表每一桢跟新一个画面)<BR> private int animcount=0;//
/动画桢更新计数器<BR> public GameObject(Image img,int width,int
height){<BR> sprite=new
Sprite(img,width,height);<BR> reset();<BR> }<BR> public void move(int
dx,int dy){//相对移动<BR> sprite.move(dx,dy);<BR> }<BR><BR> public void
moveto(int x,int
y){//绝对移动<BR> sprite.setPosition(x,y);<BR> }<BR><BR> public void
update(){//更新状态,动画桢更新,生命周期更新<BR> if(!alive)<BR> return;<BR> if(++animcount>speed){<BR> animcount=0;<BR> sprite.nextFrame();<BR> if(lifetime!=0
&&
++lifecount>lifetime)<BR> alive=false;<BR> }<BR> }<BR><BR> public
void paint(Graphics
g){//Paint<BR> if(!alive)<BR> return;<BR> sprite.paint(g);<BR> }<BR><BR> public
void
reset(){//复位<BR> alive=true;<BR> lifecount=0;<BR> animcount=0;<BR> sprite.setFrame(0);<BR> }<BR>}<BR></TD></TR></TBODY></TABLE><BR> 3.封装字体类,你需要漂亮的字体吗?<BR><BR> 我们经常需要用图片来输出文字,一个方便的字体类是必须的。我们希望仅仅提供一个图片,一个图片所描述的字符的数组,来初始化一个字体类。字体类提供一个类似Textout的方法,方便得在一个位置输出信息。先封装一个简单的版本,只支持英文和数字,并且输出不能自动换行。可能你有一个简单的思路,就是简单的保存字符数组,当打印的时候遍历数组,来查找每个字符在sprite的frameseq中的index,但当我们打印一个字符串的时候就会发现,太多的遍历操作耽误了宝贵时间,这里我们使用一个小技巧用容量换取速度,我们知道Character.
hashCode()可以返回字符的ascii编码,常用字符将返回1-127;利用这一点,我们开辟一个128的数组charhash,将输入的字符c所在图片index存入charhash[c.
hashCode()]中。以后也用这种映射方法来读取字符。charhash的元素初值为-1,以后只要数值大于0就是有效值。<BR><BR>
<TABLE borderColor=#ffcc66 width="90%" align=center bgColor=#e6e4dd border=1>
<TBODY>
<TR>
<TD>public class Font {<BR> Sprite sprite; //Sprite<BR> int width,height;
//每个char的尺寸<BR> int[] charhash;
//储存1-127个常见字符在sprite的frameseq中的位置<BR> Graphics g;<BR> public
Font(Graphics g,Image img, int width, int height, char[] chars)
{<BR> this.g=g;<BR> sprite=new
Sprite(img,width,height);<BR> this.width=width;<BR> this.height=height;<BR> charhash=new
int[128];<BR> for (int i = 0; i < charhash.length; i++)
{<BR> charhash[i]=-1;//没有代表此字符的图片<BR> }<BR> Character c;<BR> for (int
i = 0; i < chars.length; i++) {<BR> c=new
Character(chars[i]);<BR> charhash[c.hashCode()]=i;<BR> }<BR> }<BR>public
void drawChar(char ch, int x, int y){<BR> Character c=new
Character(ch);<BR> int
hashcode=c.hashCode();<BR> sprite.setPosition(x,y);<BR> if(hashcode>=0){<BR> sprite.setFrame(charhash[hashcode]);<BR> sprite.paint(g);<BR> }<BR>}<BR><BR>public
void drawString(String str, int x, int y){<BR> int
length=str.length();<BR> for (int i = 0; i < length; i++)
{<BR> drawChar(str.charAt(i),x+width*i,y);<BR> }<BR>}<BR><BR>}</TD></TR></TBODY></TABLE><BR> 这样只要有一个实例font,就可以调用font.drawString(“hello”,0,0);<BR><BR> 在0,0位置输出漂亮的图片字符串。怎么样还挺好使的吧。<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 = 1871370;
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" } else { url = "http://comments.yesky.com/l/" + latest_comments_id[i] + "/" + 0 + "/" + latest_comments_type[i] + "," + channelid + "/" + latest_comments_rfid[i] + ".shtml" } str += " <td><a href=\"" + url + "\" target=\"_blank\">" + latest_comments_contents[i].replace(re,"<img src=/TLimages/smile/face$2.gif border=0 align=middle> ") + "</a></td>" +"\n"+ " <td><font color=\"#CC6600\">" + latest_comments_postdate[i] + "</font></td>" +"\n"+ " <td nowrap=\"\" ><font color=\"#FF3300\">" + latest_comments_poster[i] + "</font></td>" +"\n"+
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -