J2ME 中高效的游戏循环

发布于 2024-07-24 09:38:02 字数 754 浏览 8 评论 0原文

我已经回答了一系列有关 J2ME 游戏开发的问题,在最近的一个问题中,Neil Coffey 评论道

作为一个附带问题——你真的想要吗 在 J2ME 游戏中每秒执行 100 个刻度? 正如我认为某人提到的,你 真该还睡到下 所需的唤醒点,不是固定的 每次持续时间。

出于某种原因,这件事在我脑海中挥之不去,现在我想要答案,我需要什么才能让我的游戏循环睡眠到所需的唤醒点,我真的不知道它在哪里点xD(理论上来说)。

为了提供反馈,这是我的游戏循环的简化版本:

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);
        }
    }
}

感谢您的帮助!

I've done a series of questions about J2ME Game developing, and in a recent one, Neil Coffey commented

As a side issue-- do you REALLY want
to do 100 ticks/second in a J2ME game?
As I think sb has mentioned, you
should really also sleep to the next
desired wake-up point, not a fixed
duration each time.

For some reason, that thing stuck in my mind and now I want the answers, what do I need to make my gameloop sleep to the desired wake-up point, I don't really know where is that point xD (theoretically speaking).

For the sake of feedback, this is a simplified version of my 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);
        }
    }
}

Thanks for your help!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陈甜 2024-07-31 09:38:02

如果您还没有阅读过此处,有一篇关于游戏循环的精彩文章。

根据我编写 J2ME 游戏的经验,您需要尽量减少在游戏循环中执行任何操作的次数,因为编写得不好的游戏循环会很快耗尽手机的微薄电池。 也就是说,如果您每 10 毫秒渲染一次,请务必检查是否有任何更改,如果没有,请跳过它。

There is an excellent article on game loops here, if you haven't read it already.

From my experience writing J2ME games, you'll want to minimize how much you do anything in a game loop, as a poorly written game loop can quickly drain a phones puny battery. That is, if you're rendering ever 10 milliseconds, make sure to check to see if anything has changed, and if not, skip it.

自此以后,行同陌路 2024-07-31 09:38:02

如果您决定每 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 毫秒的结果)。 这是平台相关的。

If you decide that you want to render every 10 milliseconds, then you:

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
}

I understand that there may be issues with timer resolution (for example, Thread.sleep(10) may not sleep for exactly 10 ms; and System.currentTimeMillis() may not return results with granularity down to 1 ms). This is platform dependent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文