另外,如果游戏中可以不使用线程
(
比如
nokia
的手机就可以不用
)
,也尽量不要用。因为
java
虚拟机中本身也有个时钟周期
(
此时钟周期非
CPU
的时钟周期,只是我随便叫个名字而已
)
,并且比线程的时钟周期精确度高。
比如在
nokia
平台
用
Thread:
int threadDelay;//
帧时间
public class GameCanvas extends FullCanvas implements Runnable{
public void run(){
//
游戏处理
…………………….
try{
Thread.sleep(threadDelay);
}catch(Exception e){}
}
};
一般只能运行大约
10fps
(
6600
,
7610
等
symbian 7.0
平台
,N70
等
8.0
平台可能会快些)。
如果这样做
:
long nextTime, currentTime;
int threadDelay;//
帧时间
Boolean isThreadRun = true;
public void loopRun(){
nextTime = System.currentTimeMillis();
while(isThreadRun){
currentTime = System.currentTimeMillis();
if(currentTime>=nextTime){
//
游戏处理
………………..
timeProcess = System.currentTimeMillis() - currentTime;
if(timeProcess>=threadDelay){
nextTime = currentTime+timeProcess+1;
}else nextTime = currentTime+threadDelay;
}
}
}
那么可以运行到
13fps
(
6600
,
7610
等
symbian 7.0
平台)
,
速度提升非常客观。
下面说一下我的调色板游戏实现方法
:
在
J2me
平台上,可以用
midp2.0
的
Graphics.drawRGB
将象素绘画到屏幕上
(midp1.0
中就只能使用厂商的
api
了,比如
nokia
的
dg.drawPixels())
,一般这个只需要调用一次,也就是将象素贴到屏幕上。
游戏中使用两种图片
:
(为了容易理解,我用类进行封装)
1.
对应
Graphics.drawRGB
的屏幕位图
:
class RGBImage{
public int imageWidth;
public int imageHeight;
public int[] imageData;//
长度
= imageWidth* imageHeight;
};
2.
对应调色板的图片集合
class PalletteImages {
public int[128] palette;
public int imageNumbers;
public int[] imageSize;
public int[]
imageDataFromIndex;
public byte[] data;
};
为什么要调色板为
128
呢?因为
java
中的
byte
为
-127 - +127
,要用
256
色就必须算加法,如果不想用,就只能用
0-127
,所以就定义为
128
的长。根据我的使用经历,
128
色完全够用。
ImageSize
存储每一张图片的大小,例如:总共有
20
张图,那么
imageSize
的长度就起码为
20*2 = 40
。第
n
张图的尺寸宽度
=imageSize[n*2],
高度
= imageSize[n*2+1]
。
ImageDataFromIndex存储每一张图片在data数据中的开始位置。
data
存储图片的索引数据,存储为如下格式
:
图片
1
索引
1
,图片
1
索引
2
,
……….
图片
1
索引
n,
图片
2
索引
1
,图片
2
索引
2
,
……….
图片
2
索引
m,
.
.
.
图片
d
索引
1
,图片
d
索引
2
,
……….
图片
d
索引
g