2008-06-30

游戏主线程

该游戏的主线程处于继续自Canvas有类中:
/**
 * 游戏线程
 */

// 每33毫秒一个循环(即每秒24帧)
private final static int MIN_LOOP_TIME = 33;
// 每5步刷新一次屏幕
private final static int MIN_REPAINT_LOOP = 5;

public final void run() {
	Thread.currentThread().setPriority(1);
	time = System.currentTimeMillis();
	try {
		while (true) {
			//... 部分省略
			viewMan.go();//viewMan是视图控制器,由它控制游戏的进行。

			// 一般情况下循环MIN_REPAINT_LOOP次才刷新屏幕
			// 有刷新要求除外
			if (needRepaint || loops > MIN_REPAINT_LOOP) {
				repaint();
				serviceRepaints();
				loops = 0;
			} else {
				loops++;
			}
			//nowStep++;
			// 保证一次循环最少要有MIN_LOOP_TIME,若不足则加入延时
			long l = (time + MIN_LOOP_TIME) - System.currentTimeMillis();
			if (l > (long) 0) {
				synchronized (this) {
					wait(l);
				}
				// 延时后要对屏幕进行刷新
				needRepaint = true;
			} else {
				needRepaint = false;
			}
			time += MIN_LOOP_TIME;
		}
	} catch (InterruptedException interruptedexception) {
	}
}

评论
发表评论

您还没有登录,请登录后发表评论

iwinyeah
搜索本博客
我的相册
C8609f76-ccc2-3dbc-a9e8-b5e08077b8c2-thumb
jaccount1.0.7
共 5 张
最近加入圈子
存档
最新评论