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

📄 j2me 2d小游戏入门之加入子弹群.htm

📁 这是我整理的关于j2me飞机游戏制作材料,对入门有很大的帮助的
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<DIV id=span_m2per1_w1><A class=headenglish 
href="http://soft.yesky.com/os/lin/">Linux</A></DIV>
<DIV id=span_m2per1_w1><A class=headblack-s 
href="http://design.yesky.com/">设计</A></DIV>
<DIV id=span_m2per1_w1><A class=headenglish 
href="http://design.yesky.com/photoshop/">Photoshop</A></DIV>
<DIV id=span_m2per1_w1><A class=headblack-s 
href="http://telecom.chinabyte.com/NetCom/218449874989875200/index.shtml">交换</A></DIV>
<DIV id=span_m2per1_w1><A class=headblack-s 
href="http://dev.yesky.com/">开发</A></DIV>
<DIV id=span_m2per1_w1><A class=headenglish 
href="http://dev.yesky.com/msdn/">MSDN</A></DIV>
<DIV id=span_m2per1_w1><A class=headblack-s 
href="http://soft.yesky.com/security/">安全</A></DIV>
<DIV id=span_m2per1_w1><A class=headblack-s 
href="http://edu.yesky.com/edupxpt">认证</A></DIV>
<DIV id=span_m2per1_w1><A class=headblack-s 
href="http://bbs.yesky.com/">论坛</A></DIV></DIV>
<DIV class=left><IMG src="J2ME 2D小游戏入门之加入子弹群.files/index-nav16.gif" 
border=0></DIV>
<DIV class=left><IMG src="J2ME 2D小游戏入门之加入子弹群.files/index-nav17.gif" 
border=0></DIV>
<DIV id=szsh><A class=headblack-s href="http://i.yesky.com/"><SPAN 
style="COLOR: #d70418">数字生活</SPAN></A></DIV>
<DIV class=headbg2_2>
<DIV id=span_m2per2_w1><A class=headblack 
href="http://e.yesky.com/">e时代</A></DIV>
<DIV id=span_m2per2_w1><A class=headblack 
href="http://game.yesky.com/">游戏</A></DIV>
<DIV id=span_m2per2_w1><A class=headblack 
href="http://pic.yesky.com/">图片</A></DIV>
<DIV id=span_m2per2_w2><A class=headblack-s 
href="http://hot.yesky.com/">网摘</A></DIV>
<DIV id=span_m2per2_w2><A class=headblack-s 
href="http://blog.yesky.com/">博客</A></DIV>
<DIV id=span_m2per2_w1><A class=headblack 
href="http://www.mydown.com/">下载</A></DIV></DIV>
<DIV class=left><IMG src="J2ME 2D小游戏入门之加入子弹群.files/index-nav16.gif" 
border=0></DIV></DIV><!--第三栏导航-->
<DIV class=box>
<DIV class=left><IMG src="J2ME 2D小游戏入门之加入子弹群.files/index-nav19.gif" 
border=0></DIV>
<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>&gt;<A 
href="http://soft.yesky.com/"> 软件</A>&gt;<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>作者: 朱晓华 </DIV>
<DIV class=newtitle2>出处: J2ME开发网 </DIV>
<DIV class=newtitle3>责任编辑: 方舟 </DIV>
<DIV class=newtitle4>[ 2004-11-06 14:40 ]</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>  飞机类游戏中子弹是必不可少的,他们数量很多且充斥着整个屏幕,这些随机或者有着一定AI的小物体,实现起来不是总那么容易,有时候你不得不考虑很多和效能有关的问题。我们之前定义了GameObject,很大程度上就是为了方便的重用Sprite,因为我们有很多的子弹,不可能没增加一个子弹都是一个Sprite,我需要共享同一个Sprite。我们通过继承GameObject来实现。 
<BR><BR>  下面分析一下这个子弹类:<BR><BR>  它将继承自GameObject;<BR><BR>  记录子弹的个数;<BR><BR>   一个子弹的状态数组,记录各个子弹的类型type,位置x,y,速度vx,vy,是否存活alive等等。<BR>   <BR>  初始化子弹<BR><BR>  一个绘制方法,将子弹画到屏幕上。<BR><BR>  一个碰撞检测方法。<BR><BR>  好了先这样吧,以下是我们子弹类的定义,注意这种思想——重用Sprite,这很重要。(这里参考了tony的很多设计)<BR><BR>
<TABLE borderColor=#ffcc66 width="90%" align=center bgColor=#e6e4dd border=1>
  <TBODY>
  <TR>
    <TD>public class Bullets extends GameObject {<BR><BR> private int[][] 
      bullets;//子弹状态数组<BR> private int bulletstotal;//数组的length<BR> private 
      Random rnd;//随机数<BR> public static final int 
      BULLET_TYPE_LEFT=0;//子弹初始化的位置类型<BR> public static final int 
      BULLET_TYPE_RIGHT=1;//分为上下左右四种<BR> public static final int 
      BULLET_TYPE_TOP=2;<BR> public static final int 
      BULLET_TYPE_BOTTOM=3;<BR> private int 
      width,height;//屏幕的高和宽,用于随机子弹位置<BR> public Bullets(Image img,int 
      picwidth,int picheight,int bulletstotal,int width,int height) 
      {<BR>  super(img,picwidth,picheight);<BR>  this.bulletstotal=bulletstotal;<BR>  bullets=new 
      int[bulletstotal][6];<BR>  rnd=new 
      Random();<BR>  this.width=width;<BR>  this.height=height;<BR> }<BR><BR> public 
      void initBullets(){//初始化子弹状态数组<BR>  for (int i = 0; i &lt; bullets.length; 
      i++) {<BR>   initBullet(i);<BR>  }<BR> }<BR><BR> private void 
      initBullet(int i) {//初始化index号子弹<BR>  bullets[i][0] = (rnd.nextInt() &amp; 
      0x7fffffff) % 4; //type<BR>  bullets[i][5] = 1; //alive 1表示存活, 
      0表示死去<BR>  switch (bullets[i][0]) {<BR>   case 
      BULLET_TYPE_LEFT:<BR>    bullets[i][1] = -5;<BR>    bullets[i][2] = 
      (rnd.nextInt() &amp; 0x7fffffff) % height;<BR>    bullets[i][3] = 
      (rnd.nextInt() &amp; 0x7fffffff) % 3 + 1; //vx<BR>    bullets[i][4] = 
      (rnd.nextInt()) % 3; //vy<BR>    break;<BR>   case 
      BULLET_TYPE_RIGHT:<BR>    bullets[i][1] = width + 5;<BR>    bullets[i][2] 
      = (rnd.nextInt() &amp; 0x7fffffff) % height;<BR>    bullets[i][3] = ( 
      (rnd.nextInt() &amp; 0x7fffffff) % 3 + 1) * -1; //vx<BR>    bullets[i][4] 
      = (rnd.nextInt()) % 3; //vy<BR>    break;<BR>   case 
      BULLET_TYPE_TOP:<BR>    bullets[i][1] = (rnd.nextInt() &amp; 0x7fffffff) % 
      width;<BR>    bullets[i][2] = -5;<BR>    bullets[i][3] = (rnd.nextInt()) % 
      3; //vx<BR>    bullets[i][4] = (rnd.nextInt() &amp; 0x7fffffff) % 3 + 1; 
      //vy<BR>    break;<BR>   case BULLET_TYPE_BOTTOM:<BR>    bullets[i][1] = 
      (rnd.nextInt() &amp; 0x7fffffff) % width;<BR>    bullets[i][2] = height + 
      5;<BR>    bullets[i][3] = (rnd.nextInt()) % 3; //vx<BR>    bullets[i][4] = 
      ( (rnd.nextInt() &amp; 0x7fffffff) % 3 + 1) * -1; 
      //vy<BR>    break;<BR>   }<BR>  }<BR>  public void updata(int 
      i){//根据速度更新i子弹下一桢的位置,碰壁反弹<BR>   bullets[i][1]+=bullets[i][3];<BR>   bullets[i][2]+=bullets[i][4];<BR>   if(bullets[i][1]&lt;-5 
      || 
      bullets[i][1]&gt;width+5){<BR>    bullets[i][3]*=-1;<BR>   }<BR>   if(bullets[i][2]&lt;-5 
      || 
      bullets[i][2]&gt;height+5){<BR>    bullets[i][4]*=-1;<BR>   }<BR>  }<BR>  private 
      void paint(Graphics g,int 
      i){//绘画出第i个子弹<BR>   updataspritepos(i);//更新位置<BR>   sprite.paint(g);//绘画Sprtie<BR>  }<BR>  public 
      void paint(Graphics g) {//绘画整个子弹组<BR>   for (int i = 0; i &lt; 
      bullets.length; i++) 
      {<BR>    if(bullets[i][5]==0){//死去的子弹不绘画<BR>     continue;<BR>    }<BR>   sprite.setPosition(bullets[i][1],bullets[i][2]); 
      //更新位置<BR>   sprite.paint(g);<BR>  }<BR> }<BR> public void 
      refreshBullets(Sprite planesprite, boolean 
      needcollision){//刷新字典数组的状态,并作碰撞处理<BR>  for (int i = 0; i &lt; 
      bullets.length; i++) {<BR>   if(bullets[i][5]==0){ 
      //死去的子弹不更新<BR>    continue;<BR>  }<BR>  if(needcollision){//如果需要碰撞检测<BR>   if 
      (isCollision(planesprite, i, 10)) 
      {//如果碰撞,进行处理<BR>    //System.out.println("collision 
      ");<BR>    Navigate.mc.gameover = 
      true;<BR>    Navigate.mc.explosion.sprite.setPosition(bullets[i][1] - 
      16,bullets[i][2] - 16);<BR>    bullets[i][5] = 
      0;//杀死碰撞的子弹<BR>    continue;<BR>   }<BR>  }<BR>  updata(i);//更新状态<BR> }<BR>}<BR><BR>private 
      boolean isCollision(Sprite sprite,int i,int 
      range){<BR> //判断是否碰撞<BR> //updataspritepos(i);<BR> //return 
      sprite.collidesWith(this.sprite,true);<BR> boolean result=false;<BR> int 
      planeXCenter=sprite.getX()+12;<BR> int 
      planeYCenter=sprite.getY()+12;<BR> int 
      bulletXCenter=bullets[i][1]+3;<BR> int 
      bulletYCenter=bullets[i][2]+3;<BR> if(Math.abs(planeXCenter-bulletXCenter) 
      &lt; range){<BR>  if (Math.abs(planeYCenter - bulletYCenter )&lt; range) 
      {<BR>   result = true;<BR>  }<BR> }<BR> return result;<BR>}<BR><BR>private 
      void updataspritepos(int 
      i){//将sprite更新到i字弹的位置<BR><BR>sprite.setPosition(bullets[i][1],bullets[i][2]);<BR><BR>}<BR><BR>public 
      void killbullets(Sprite planesprite,int range){杀死一定区域内的子弹<BR><BR> for (int 
      i = 0; i &lt; bullets.length; i++) {<BR>  if(bullets[i][5]!=0){//alive 
      bullets<BR>   if(isCollision(planesprite, i, 
      range)){<BR>    bullets[i][5]=0;<BR>    initBullet(i);<BR>   }<BR>  }<BR> }<BR>}<BR><BR>}</TD></TR></TBODY></TABLE><BR>  子弹如何表示?<BR><BR>  首先我们用一个二维数组来记录子弹的信息:<BR><BR>  bullets[i][0]表示子弹的类型,有上、下、左、右四种,分别表示子弹飞入屏幕前的四种位置;<BR><BR>  bullets[i][1]表示子弹的x坐标;<BR><BR>  bullets[i][2]表示子弹的y坐标<BR><BR>  bullets[i][3]表示子弹的x方向速度;<BR><BR>  bullets[i][4]表示子弹的y方向速度;<BR><BR>  bullets[i][5]表示子弹的存活状态;<BR><BR>  子弹如何初始化?<BR><BR>  我们首先写了一个初始化单个子弹的方法,然后便利数组调用initBullet 
(i);来更新整个状态数组。<BR><BR>  子弹如何绘制?<BR><BR>  我们首先写了一个绘制单个子弹的方法,然后便利数组调用paint(g,i);来绘制整个状态数组。<BR><BR>  子弹如何碰撞?<BR><BR>  有很多种方法,其中sprite本身就提供了边框碰撞检测和基于像素的碰撞检测。前者不太适合我们的游戏,我们的飞机是不规则物体,且飞行游戏对碰撞比较敏感;而后者的效率又得不到我们的信赖,所以我们是用一种半径检测,把飞机近似的看成圆,选取恰当的半径,Math.abs(planeXCenter-bulletXCenter) 
&lt; 
range则表明碰撞。<BR><BR>  碰撞看似简单,其实是很复杂的问题,值得庆幸的是,二维碰撞相比三维碰撞简单得多。一个小技巧是,宁可让膨胀检测半径变小也不要他变得大——漏掉检测,总比误检测要好得多。<BR><BR>  子弹更新?<BR><BR>  我们利用refreshBullets进行更新,这是主要逻辑部分。这个方法负责便利数组检测碰撞,如果碰撞就将处于碰撞位置的子弹杀死,并作相应的处理,这里是结束游戏并爆炸飞机;否则更新子弹的位置。<BR><BR>  我们只是线性的遍历整个的数组,进行碰撞检测,之后是更新位置;但是这样做有一个前提,就是碰撞检测简单而且处理部分也很简单:在这个游戏中,碰撞检测只是子弹群和飞机的检测,碰撞检测在游戏结束后就不执行了(通过控制boolean 
needcollision);而处理更是简单了一些——直接结束了游戏。如果不是如此,比如处理后并不是简单的结束游戏,我们就不得不设计的复杂一些。可能就不是将碰撞简单的以飞机为中心了。我们需要设计好游戏事件,设计好碰撞系统。<BR><BR>  如果碰撞本身比较复杂,或者子弹数量,种类增加时,我们线性的遍历数组就不能总是对所有的子弹都检测,可能屏幕需要分区,不处于一个区域的单位不检测。<BR><BR>  总之当你想想你的1934时,将不在是想象着子弹,飞机什么的,你要思考一个系统。<BR><BR>  总结一下子弹类的公共接口:<BR><BR>   
Bullets(Image img,int picwidth,int picheight,int bulletstotal,int width,int 
height)构造函数<BR><BR>   public void initBullets()初始化子弹数组<BR><BR>   public void 
paint(Graphics g) paint子弹数组<BR><BR>   public void refreshBullets(Sprite 
planesprite, boolean needcollision)更新子弹数组状态,碰撞检测、处理等逻辑工作的综合<BR><BR>   public 
void killbullets(Sprite planesprite,int 
range)//稍后解释<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 = 1872698;
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>&nbsp;<A class=red-s href="http://photo.yesky.com/" 
target=_blank>上传相片</A>&nbsp;<A class=red-s href="http://bbs.yesky.com/" 
target=_blank>论坛聊天</A>&nbsp;<A class=red-s href="http://bbs.yesky.com/" 
target=_blank>订阅电子杂志</A>&nbsp;<A class=red-s 
href="http://www.yeecai.com/mobileant/tjadd/tjload.html" 
target=_blank>彩信蚂蚁</A>&nbsp;<A class=red-s 
href="http://hot.yesky.com/">推荐网摘</A>&nbsp;<A class=red-s 
href="http://training.yesky.com/">IT培训</A>&nbsp;<A class=red-s 

⌨️ 快捷键说明

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