使用MIDP2.0开发游戏(6)设计Clock
2005-11-29 16:12
537 查看
文章来源:J2ME开发网
Clock负责提供一个真实时间和一个虚拟时间,真实时间从0开始按ms递增,和硬件时钟是同步的;虚拟时间也从0开始按ms递增,但不一定和真实时间同步。
要获得系统时间可以用System.currentTimeMillies(),系统硬件有一个计数器,当计算机启动时,计数器从0开始每1ms加1,System.currentTimeMillies()返回从开机到现在经过的ms。我们不需要知道时分秒,只需要一个递增的整数计时就可以了。
Clock改自Marshall "Game Programming Gems 3"中的C++代码,主要成员变量:
thisTime:当前硬件时间,即System.currentTimeMillies()
systemTime:游戏的系统时间,即把thisTime转换为从0递增的时间
virtualTime:虚拟时间,从0递增,但和真实时间不同步
代码如下:
Clock负责提供一个真实时间和一个虚拟时间,真实时间从0开始按ms递增,和硬件时钟是同步的;虚拟时间也从0开始按ms递增,但不一定和真实时间同步。
要获得系统时间可以用System.currentTimeMillies(),系统硬件有一个计数器,当计算机启动时,计数器从0开始每1ms加1,System.currentTimeMillies()返回从开机到现在经过的ms。我们不需要知道时分秒,只需要一个递增的整数计时就可以了。
Clock改自Marshall "Game Programming Gems 3"中的C++代码,主要成员变量:
thisTime:当前硬件时间,即System.currentTimeMillies()
systemTime:游戏的系统时间,即把thisTime转换为从0递增的时间
virtualTime:虚拟时间,从0递增,但和真实时间不同步
代码如下:
package game.engine.core; class Clock { // Clock是否运行: private boolean running; // 当前hardware clock: private int thisTime; // record the last hardware clock when calling stop(): private int lastTime; // systemTime从0开始递增,和硬件时钟同步: private int systemTime; // systemOffset就是硬件时钟和systemTime的差: private int systemOffset; // 上一次停止的systemTime: private int pauseAt; // virtualTime starts from 0. private int virtualTime; // virtualOffset records how long the clock paused: private int virtualOffset; private int frameStart; private int frameEnd; private int frameCount; public Clock() { reset(); } // 重置Clock: public void reset() { running = false; // get the hardware clock: thisTime = (int)System.currentTimeMillis(); lastTime = thisTime; // and systemTime starts from 0: systemTime = 0; systemOffset = thisTime; pauseAt = 0; // init virtual time: virtualTime = 0; virtualOffset = 0; // init frame time: frameStart = 0; frameEnd = 0; frameCount = 0; } // 同步hardware clock: private void update() { lastTime = thisTime; thisTime = (int)System.currentTimeMillis(); // increase the systemTime: systemTime += (thisTime - lastTime); } // 启动Clock: public void start() { if(!running) { running = true; update(); virtualOffset += (systemTime - pauseAt); System.out.println("[start]"); } } // 停止Clock: public void stop() { if(running) { running = false; update(); pauseAt = systemTime; System.out.println("[stop] at " + pauseAt); } } // Clock是否运行: pu
相关文章推荐
- 使用MIDP2.0开发游戏 (6) 设计Clock
- 使用MIDP2.0开发游戏 (7) 设计Scheduler
- 使用MIDP2.0开发游戏(7)设计Scheduler
- 使用MIDP2.0开发游戏(4)改进的地图生成方式
- 使用MIDP2.0开发游戏 (5) 游戏内核
- 使用MIDP2.0开发游戏
- 使用MIDP2.0开发游戏(5)游戏内核
- 使用MIDP2.0开发游戏 (2) 使用Sprite
- 使用MIDP2.0开发游戏 (1) GameCanvas基础
- 使用MIDP2.0开发游戏(1)GameCanvas基础
- 使用MIDP2.0开发游戏(2)使用Sprite
- 使用MIDP2.0开发游戏 (2) 使用Sprite
- 使用MIDP2.0开发游戏(3)添加背景和前景
- 使用MIDP2.0开发游戏 (4) 改进的地图生成方式
- 使用MIDP2.0开发游戏 (3) 添加背景和前景
- [libGDX游戏开发教程]使用libGDX进行游戏开发(1)-游戏设计
- 使用 Unity 3D 开发游戏的架构设计难点
- [libGDX游戏开发教程]使用LibGDX进行游戏开发(1)-游戏设计
- 使用 Unity 3D 开发游戏的架构设计难点
- 【Cocos2d-X(2.x) 游戏开发系列之三】最新版本cocos2d-2.0-x-2.0.2使用资源加载策略