限制FPS在Libgdx游戏视频下载()不工作 [英] Limit FPS in Libgdx game with Thread.sleep() doesn't work

查看:481
本文介绍了限制FPS在Libgdx游戏视频下载()不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个小游戏为Android与libgdx并希望将FPS限制为30,以节省电池。的问题是,它无法工作。在FPS刚刚从60下降到56。

I am developing a little game for android with libgdx and want to limit the fps to 30 to save battery. The problem is that it doesn't work. The fps just drops from 60 to 56.

这里是code部分:(这是在渲染部分的结尾)

Here is the part of the code: (it's at the end of the render part)

System.out.print("\nFPS: " + Gdx.graphics.getFramesPerSecond() + "\n");

    if(Gdx.graphics.getDeltaTime() < 1f/30f)
    {
        System.out.print("DeltaTime: " + Gdx.graphics.getDeltaTime() + " s\n");
        float sleep = (1f/30f-Gdx.graphics.getDeltaTime())*1000;
        System.out.print("sleep: " + sleep + " ms\n");
        try
        {
            Thread.sleep((long) sleep);
        }
        catch (InterruptedException e)
        {
            System.out.print("Error...");
            e.printStackTrace();
        }
    }

和这里是输出:

FPS: 56
DeltaTime: 0.014401722 s
sleep: 18.931612 ms

FPS: 56
DeltaTime: 0.023999143 s
sleep: 9.334191 ms

FPS: 56
DeltaTime: 0.010117603 s
sleep: 23.215733 ms

在此先感谢...

推荐答案

使用可能会利用此

Thread.sleep((long)(1000/30-Gdx.graphics.getDeltaTime()));

修改30值FPS你想要的。

change the value of 30 to FPS you want.

这篇关于限制FPS在Libgdx游戏视频下载()不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆