服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

用netbeans开发平台开发j2me游戏实例讲解(第四部分)


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);

        }

       

        其中offsetbyte[]的偏移量,一个整型量用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();