时间效率Gameloop在J2ME

问题描述:

我已经做了一系列关于J2ME游戏开发的问题,并在最近一,尼尔·科菲评论时间效率Gameloop在J2ME

作为一个边issue--你真的想 做J2ME游戏中的每秒100个ticks /秒? 正如我认为某人已经提到的那样,你应该真的睡到下一个 期望的唤醒点,而不是每个固定的持续时间。

出于某种原因,停留在我的脑海里的那个东西,现在我想要的答案,我需要什么,使我gameloop睡眠到期望唤醒点,我真的不知道在哪里这点xD(理论上讲)。

对于反馈的缘故,这是我gameloop的简化版本:

public void run() { 
    Graphics g = this.getGraphics(); 
    while (running) { 
     long diff = System.currentTimeMillis() - lastLoop; 
     lastLoop = System.currentTimeMillis(); 
     input(); 
     this.level.doLogic(); 
     render(g, diff); 
     try { 
      Thread.sleep(10); 
     } catch (InterruptedException e) { 
      stop(e); 
     } 
    } 
} 

感谢您的帮助!

如果决定要渲染每10毫秒,那么你:

loop { 
    - record current time; render 
    - do input/logic 
    - check the current time, and calculate elapsed time 
    - if less than 10 ms has elapsed, calculate the remaining time (10 millis - elapsed time); sleep for this duration 
} 

据我所知,可能有计时器的分辨率问题(例如,的Thread.sleep(10)可以不睡觉正好10毫秒;而System.currentTimeMillis()可能不会返回粒度降至1毫秒的结果)。这取决于平台。

如果您还没有阅读过关于游戏循环here的优秀文章,

从我写J2ME游戏的经验来看,你会希望尽量减少你在游戏循环中做了多少事情,因为写得不好的游戏循环可能会很快耗尽电话中不精确的电池。也就是说,如果你正在渲染10毫秒,请确保检查是否有任何更改,如果没有,请跳过它。