饒榮慶 -- 您今天UCWEB了嗎?--http://www.ucweb.com

          3G 手機開發網

             :: 首頁 :: 聯系 :: 聚合  :: 管理
            99 Posts :: 1 Stories :: 219 Comments :: 0 Trackbacks
          設計游戲我個人認為一個好的角色操作將事半工倍。所以我現在把雷電的所有角色抽象成一個Fairy。里面有實現繪制的方法以及移動,碰撞算法。

          ????? 在這里我強調下,我的碰撞算法是簡單的實現。就是一個物體為參照物體。在10像素范圍內x,y軸如果發現另外一個物體侵入則判斷為true,發之為false

          java 代碼


          ?


          1. ???

          2. package?org.wuhua.game.model;??

          3. ??

          4. import?javax.microedition.lcdui.Graphics;??

          5. import?javax.microedition.lcdui.Image;??

          6. ??

          7. import?org.wuhua.game.util.Log;??

          8. ???

          9. /**?

          10. ?*?類名:Sprite.java?

            ?
            ?

          11. ?*?編寫日期:?2006-11-29?

            ?

          12. ?*?程序功能描述:建立精靈物體模型?

            ?

          13. ?*?Demo:?

            ?

          14. ?*?Bug:?

            ?

          15. ?*??

          16. ?*?程序變更日期?:

            ?
            ?

          17. ?*?變更作者?:

            ?
            ?

          18. ?*?變更說明?:

            ?

          19. ?*??

          20. ?*?@author?wuhua?

            ?
            ?

          21. ?*/??

          22. public?class?Fairy?{??

          23. ????static?Log?log?=?Log.getLog("Fairy");??

          24. ????/**?

          25. ?????*?position?of?Fairy?in?x?offset??

          26. ?????*/??

          27. ????int?x;?//?=?0;??

          28. ??

          29. ????/**?

          30. ?????*?position?of?Fairy?in?y?offset??

          31. ?????*/??

          32. ????int?y;?//?=?0;??

          33. ??

          34. ????/**?

          35. ?????*?width?of?layer??

          36. ?????*/??

          37. ????int?width;?//?=?0;??

          38. ??

          39. ????/**?

          40. ?????*?height?of?layer?

          41. ?????*/??

          42. ????int?height;?//?=?0;??

          43. ??

          44. ????/**??

          45. ?????*?If?the?Layer?is?visible?it?will?be?drawn?when?paint?

          46. ?????*?is?called.?

          47. ?????*/??

          48. ????boolean?visible?=?true;??

          49. ??????

          50. ????/**?

          51. ?????*?圖片資源?

          52. ?????*???

          53. ?????*/??

          54. ??????

          55. ????Image?fairy;???

          56. ??????

          57. ????public?Fairy(Image?fairy,int?x,?int?y){??

          58. ????????this.fairy?=?fairy;??

          59. ????????this.x?=?x;??

          60. ????????this.y?=?y;??

          61. ????}??

          62. ??????

          63. ????public?void?setPosition(int?x,?int?y)?{??

          64. ????????this.x?=?x;??

          65. ????????this.y?=?y;??

          66. ????}??

          67. ??????

          68. ????public?void?move(int?dx,?int?dy)?{????

          69. ???????

          70. ????????x?+=?dx;??

          71. ????????y?+=?dy;??

          72. ????}??

          73. ??????

          74. ????public?void?setVisible(boolean?visible)?{??

          75. ????????this.visible?=?visible;??

          76. ????}??

          77. ??

          78. ?????

          79. ????public?final?boolean?isVisible()?{??

          80. ????????return?visible;??

          81. ????}??

          82. ??

          83. ????public?final?int?getHeight()?{??

          84. ????????return?height;??

          85. ????}??

          86. ??

          87. ????public?final?int?getWidth()?{??

          88. ????????return?width;??

          89. ????}??

          90. ??

          91. ????public?final?int?getX()?{??

          92. ????????return?x;??

          93. ????}??

          94. ??

          95. ????public?final?int?getY()?{??

          96. ????????return?y;??

          97. ????}??

          98. ??????

          99. ????public?void?paint(Graphics?g){??

          100. ????????if?(g?==?null)?{??

          101. ????????????throw?new?NullPointerException("Graphics?不存在");??

          102. ????????}??

          103. ????????if(this.visible){??

          104. ????????????//log.debug("x="?+?x?+?"?y="?+?y);???

          105. ????????????g.drawImage(fairy,?x,?y,??Graphics.TOP?|?Graphics.HCENTER);??

          106. ????????}??

          107. ????}??

          108. ??

          109. ????/**?

          110. ?????*?進行簡單的碰撞算法,?希望高手可以給個建議。?

          111. ?????*?@param?f?

          112. ?????*?@return?

          113. ?????*/??

          114. ????public?final?boolean?collidesWith(Fairy?f){??

          115. ??????????

          116. ???????

          117. ????????if((f.getX()?>=?this.getX()?-?20?&&?f.getX()?<=?this.getX()?+?20)??

          118. ????????????????&&??(f.getY()?>=?this.getY()?-?10??&&?f.getY()?<=?this.getY()+10?)){??

          119. ????????????//log.debug("this.getY="?+?this.getY());??

          120. ????????????//log.debug("f.getY="?+?f.getY());??

          121. ???????????????

          122. ???????????????

          123. ????????????return?true;??

          124. ????????}??

          125. ??????????????

          126. ????????return?false;??

          127. ????}??

          128. ???

          129. ??

          130. }??






          爬蟲工作室 -- 專業的手機軟件開發工作室
          3G視線 -- 專注手機軟件開發
          posted on 2007-03-27 12:27 3G工作室 閱讀(1227) 評論(0)  編輯  收藏 所屬分類: j2me
          主站蜘蛛池模板: 陆丰市| 西乡县| 象山县| 漯河市| 浮梁县| 乐至县| 资溪县| 旺苍县| 洱源县| 柳林县| 承德县| 宁津县| 吉林市| 墨江| 华阴市| 江口县| 衡阳市| 依兰县| 任丘市| 富源县| 卓资县| 尖扎县| 岱山县| 新乐市| 建德市| 晴隆县| 乌兰浩特市| 兰考县| 苏尼特左旗| 华池县| 呼伦贝尔市| 横山县| 台湾省| 肇州县| 东山县| 韶山市| 泗水县| 铁力市| 盘山县| 阿勒泰市| 左权县|