您的位置:首页 > 其它

J2ME游戏“数独”开发详解(三)

2009-07-10 21:13 357 查看
继续说一下程序中分数记录与读取以及计时器功能的实现。分数的存储时通过调用j2me所提供的RecordStore类来实现的。首先看一下代码吧。public class ScoreRecordStore implements RecordComparator{private byte[] scorebyte;
RecordEnumeration re;
RecordStore rs; public ScoreRecordStore() {
scorebyte = null;
rs = RecordStore.openRecordStore("srs", true);
re = rs.enumerateRecords(null, this, false);
} public int compare(byte[] arg0, byte[] arg1) {
int compare = 0;
int score1 = Integer.parseInt(new String(arg0));
int score2 = Integer.parseInt(new String(arg1));
if (score2 > score1) {
compare = FOLLOWS;
} else {
compare = PRECEDES;
}
return compare;
} protected String getMaxScore() {
byte[] score = null;
if(re.hasNextElement()){
score = re.nextRecord();

}
if(score == null){
score ="0".getBytes();
}
return new String(score); } protected void saveScore(int score) { scorebyte = Integer.toString(score).getBytes();
rs.addRecord(scorebyte, 0, scorebyte.length);
}} 代码中ScoreRecordStore类继承了RecordCompare这个接口,目的是对所存储的数据进行比较,将分数最高的显示在画面中。它是通过compare这个方法来实现的。数据的存储空间的建立是通过 rs = RecordStore.openRecordStore("srs", true);来实现的,注意在这个openRecordStore("srs", true)方法中应将的二个属性设置为true,不过要确保srs这个名字在内存中不存在,如果设置为false的话,系统可能出现异常告诉你申请的存储仓库没有找到。 计时器的实现主要是使用了j2me提供的Timer和TimerTask两个类来实现的。TimeerTask中实现了run()方法,在run方法中调用secAdd()方法,然后通过Timer提供的
 shecule
(Timertask, long delay, long period),该方法的含义是在delay的时间之后,每隔preiod秒调用一次Timertask类,从而实现了画面中时间的变化。
代码如下:





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐