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
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类,从而实现了画面中时间的变化。
代码如下:
相关文章推荐
- J2ME游戏“数独”开发详解(一)
- J2ME游戏“数独”开发详解(二)
- 【Android游戏开发二十二】(图文详解)游戏中灵活实现动画播放!简述J2me的游戏类库与Android游戏开发!
- 【Android游戏开发二十二】(图文详解)游戏中灵活实现动画播放!简述J2me的游戏类库与Android游戏开发!
- 【Android游戏开发二十二】(图文详解)游戏中灵活实现动画播放!简述J2me的游戏类库与Android游戏开发!
- 【Android游戏开发二十二】(图文详解)游戏中灵活实现动画播放!简述J2me的游戏类库与Android游戏开发!
- 22—(图文详解)游戏中灵活实现动画播放!简述J2me的游戏类库与Android游戏开发
- (图文详解)游戏中灵活实现动画播放!简述J2me的游戏类库与Android游戏开发!
- (转)【Android游戏开发二十二】(图文详解)游戏中灵活实现动画播放!简述J2me的游戏类库与Android游戏开发!
- 【Android游戏开发二十二】(图文详解)游戏中灵活实现动画播放!简述J2me的游戏类库与Android游戏开发!
- j2me 游戏开发详解
- 【Android游戏开发二十二】(图文详解)游戏中灵活实现动画播放!简述J2me的游戏类库与Android游戏开发!
- 【Android游戏开发二十二】(图文详解)游戏中灵活实现动画播放!简述J2me的游戏类库与Android游戏开发!
- 【Android游戏开发二十二】(图文详解)游戏中灵活实现动画播放!简述J2me的游戏类库与Android游戏开发!
- 初学乍练J2me游戏开发 (二)
- 安卓开发_数独游戏基础(二)
- 【J2me 2D 游戏开发系列】Himi游戏开发J零基础学习历程
- J2ME手机游戏开发引擎的分析与设计
- 【Android游戏开发之八】游戏中添加音频-详解MediaPlayer与SoundPool的利弊以及各个在游戏中的用途!
- 【Android游戏开发十九】(必看篇)SurfaceView运行机制详解—剖析Back与Home按键及切入后台等异常处理!