国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

用NetBeans開發平臺開發J2ME游戲實例講解(第四部分)

2019-11-17 06:19:31
字體:
來源:轉載
供稿:網友


NetBeans開發平臺開發J2ME游戲實例講解(第四部分)

 lirincy@163.com 林剛 2005.07 轉載請注明出處作者

(3) 使用rms來記錄成績排行榜

    J2ME 記錄治理系統(RMS)提供了一種機制,通過這種機制,MIDlet 能夠持久存儲數據,并在以后檢索數據。我們在程序中就需要這樣的一個排行榜,

    這個排行榜在程序關閉以后仍然能夠記錄數據,rms正好滿足了這個要求,有關rms的信息,請參考:

   http://www-128.ibm.com/developerworks/cn/java/j-wi-rms/index.Html

   http://game.kongzhong.com/content/j2mecontent_730.html

   

    我們在程序中要實現排行榜的功能,需要做的事情有這樣幾件:

    A. 在選項菜單當中加入排行榜,這好辦, 在HuaRongDaoMielet.java中加入產生排行榜的代碼;

    B. 新建一個類,取名Score, 關于新建類的方法,上面的論述中有講述,我們需要的接口函數很簡單,getScore(), setScore()就可以了,注重這

       里,rms是字節數組組成的記錄體系,因此我們假如要存儲整型量,就要有整形到byte[]以及byte[]到整型量的轉換,如下:

      

        /**

        * 數組到整數.

        */

        PRivate int getInt(byte[]buf, int offset) {

                        return (buf[offset+0] & 0xff)<< 24

                    (buf[offset+1] & 0xff) << 16

                    (buf[offset+2] & 0xff) << 8

                    (buf[offset+3] & 0xff);

        }

   

        /**

        * 整數到數組.

        */

        private void putInt(byte[] buf, int offset, int value) {

                               buf[offset+0]= (byte)((value >> 24) & 0xff);

                               buf[offset+1]= (byte)((value >> 16) & 0xff);

                               buf[offset+2]= (byte)((value >>  8) &0xff);

                               buf[offset+3]= (byte)((value >>  0) &0xff);

        }

       

        其中offset是byte[]的偏移量,一個整型量用4個字節來表示。

        另外我們需要對rms存儲系統進行初始化,在構造函數當中引入open()函數,如下所示:

       

                  // 每關成績 = {int level,intmoves;}

                  private byte[] scoreRec; // 當前關成績.

                  privatestatic final int SCORE_LEN = 8;

             

                  private RecordStorestore;             //記錄存儲,沒有打開時為空.

                 

         /*

         * 構造函數.

         */

        Score() {

                              store= null;

                              scoreRec = new byte[SCORE_LEN];

                              putInt(scoreRec,0, 0);

                              putInt(scoreRec,4, 0);

           open();

        }

    

        /**

         * 打開存儲記錄

          */

        boolean open() {

    

                              try{

                                  store= RecordStore.openRecordStore("HuaRongDaoScore", true);

                              } catch (RecordStoreExceptionex) {

                              }

                         

                              if(store == null)

                                  returnfalse;

                              returntrue;

        }

                                  

                             可以看出,我們的每個記錄長度是8個字節,都是整型量,第一個是關,第二個是成績,最后的結構類似于:

                                  關     步數

                                  0        33

                                  1        98

                                  2        109

                                  3        77

                                  ...

                             最后,在這個類中最重要的兩個函數readScore()和setScore(), 分別負責讀取和設置某一關的成績。在這兩個函數當中,我們要注重在rms

                             中遍歷某個元素的方法,你需要自己設個標記TAG,來標記你的記錄,因為rms不象數組一樣,可以按照下標來索引,rms的幾個函數包括

                             getRecord()

                             getRecordSize()

                             enumerateRecords()

                             addRecord()

                             setRecord()

                             等等都要知道是做什么用的。

                             

                             /**

                             * 讀取某級別的成績.

                             */

                             public int readScore(intlevel) {

                                      try {

                                       // 查找成績

                                       RecordEnumerationenm = store.enumerateRecords(null, null, false);

                                       while(enm.hasNextElement()) {

                                           int ndx= enm.nextRecordId();

                                                if (store.getRecordSize(ndx) == SCORE_LEN) {

                                                   int l = store.getRecord(ndx, scoreRec, 0);

                                                   if (l == SCORE_LEN && getInt(scoreRec, 0) == level)

                                               return getInt(scoreRec, 4);

                                           }

                                       }

                  // 沒有這一關的成績,新建

                  putInt(scoreRec, 0, level);

                  putInt(scoreRec, 4, 0);

                  store.addRecord(scoreRec, 0, SCORE_LEN);

                  return 0;

                                       

                                                 }catch (RecordStoreException ex) {

                                                     ex.printStackTrace();

                                                     return-1;

                                                 }

                              }

                           

                              /**

                                 * 設置某級別的成績.

                                 */

                              booleansetScore(int level, int moves) {

                                       try{

                                           // 查找成績

                                           RecordEnumeration enm = store.enumerateRecords(null, null, false);

                                           while (enm.hasNextElement()) {

                                              int ndx = enm.nextRecordId();

                                             if (store.getRecordSize(ndx) ==SCORE_LEN) {

                                               int l = store.getRecord(ndx, scoreRec, 0);

                                               if (l == SCORE_LEN && getInt(scoreRec, 0) == level)

                                                   //只存儲較小的值

                                                   if ( getInt(scoreRec,4)== 0 getInt(scoreRec, 4)> moves){

                                                       putInt(scoreRec, 4, moves);

                                                       store.setRecord(ndx, scoreRec,0, SCORE_LEN);

                                                   }

                                             }

                                          }

                                // 沒有這一關的成績,新建

                                putInt(scoreRec, 0, level);

                                putInt(scoreRec, 4, moves);

                                store.addRecord(scoreRec, 0, SCORE_LEN);

                                                 }catch (RecordStoreException ex) {

                                                     ex.printStackTrace();

                                                     returnfalse;

                                                 }

                                                 return true;

                              }

               C. 最后要看看這個Score類怎樣使用,我們需要在主Midlet里面初始化它,然后在ConrolLogic.java中 設置成績。

                  首先,聲明這個類成員, 并且進行初始化:HuaRongDaoMidlet.java

                 

                            public class HuaRongDaoMidletextends MIDlet implements CommandListener{

 

           ... ...

                                privateScore score;

                               

                                /** The Form object for theOptions command */

                                privateForm optionsForm;

                                ... ...

                               

                                publicvoid startApp() {

                                    score = new Score();

                                ... ...

                               

                               

                            在程序結束時關閉它,

                                ... ...

                                publicvoid commandAction(Command c, Displayable d) {

                                    if ( c == CMD_EXIT && d == splash ){

                                       //退出

                                       score.close();



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新竹市| 苍山县| 通海县| 垦利县| 介休市| 二手房| 广东省| 石门县| 彰武县| 中山市| 盐津县| 岚皋县| 隆化县| 新建县| 孝昌县| 万盛区| 米易县| 宾川县| 保定市| 泰来县| 松原市| 手机| 东安县| 五大连池市| SHOW| 霍邱县| 和田市| 五大连池市| 玛多县| 阳东县| 石门县| 墨竹工卡县| 宾阳县| 房山区| 绥滨县| 台中市| 靖江市| 个旧市| 伊宁县| 武义县| 肃南|