LibGdx FPS 太多.如何限制?
我正在玩游戏,但我在一些(奇怪的)低端安卓设备(Galaxy S mini、Galaxy ACE)上遇到了问题,而且硬件实际上很糟糕.两台设备上的 FPS 均为 85+,而在其他设备(HTC Desire、Sony Xperia Arc S、Samsung Galaxy S、HTC Wildfire)上,FPS 为正常"(大约 60 FPS).计算机上也显示 60 FPS.显然85+ FPS太多了,因为游戏体验不同,可能会给那些玩85FPS+的玩家带来不公平的优势.
I'm doing a game and I have a problem on some (strange) low end android devices (Galaxy S mini, Galaxy ACE ), with actually bad hardware. The FPS is on both devices 85+, and on other devices (HTC Desire, Sony Xperia Arc S, Samsung Galaxy S, HTC Wildfire ) the FPS is "normal" (around 60FPS). Also 60 FPS is showing on computer too. Obviously 85+ FPS is too much, because the game experience is different and it may cause unfair advantage to those players who are playing on 85FPS+.
我想将 FPS 限制为 MAX 60.
I want to limit the FPS to MAX 60.
我已经搜索了这个论坛,并且找到了我现在要问的确切问题.使用 Thread.sleep() 限制 Libgdx 游戏中的 FPS'不工作
I've already searched this forum and I've found the exact question I'm asking right now. Limit FPS in Libgdx game with Thread.sleep() doesn't work
来自用户@daniel 的(接受的)解决方案是这样的:
The (accepted) solution from user @daniel was this:
Thread.sleep((long)(1000/30-Gdx.graphics.getDeltaTime()));
但问题是这对我不起作用.如果我使用该代码(将 30 替换为 60 ),我会得到 30-33 FPS,或者如果我只使用 Daniel 的代码,我会得到大约 15-17 FPS.为什么会这样,为什么不适合我?如何将所有设备上的 FPS 限制为 MAX 60?我在计算机和设备上测试了这段代码.
But the thing is that doesn't work for me. If I use that code (30 replaced with 60 ), I get 30-33 FPS or if I use just the Daniel's code I get around 15-17 FPS. Why is that , and why is not working for me ? How to limit FPS on all devices to MAX 60 ? I tested this code on computer, and on devices also.
非常感谢任何帮助.谢谢.
Any help HIGHLY appriciated. Thank you.
推荐答案
在你的启动器类中;
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
config.foregroundFPS = 60;
initialize(new MyGdxGame(), config);
我不知道这是新功能还是旧功能.在尝试休眠线程之前使用它.
I don't know if this is a new feature or an old one. Use this before trying to sleep the thread.
我强烈建议不要通过休眠线程来限制帧,只是为了让所有设备快速或慢速运行时保持相同的速度.使用类似的东西来移动屏幕上的东西.
I highly recommend not limiting the frames by sleeping the thread, just to the keep the motion at the same pace for all the devices running fast or slow. Use something like this to move things on the screen.
x+= speed*Gdx.graphics.getDeltaTime();
祝您在 libGDX 的冒险之旅中好运,看来您才刚刚开始.因为当我刚开始时,我也在问同样的问题:)
Good luck on your adventure with libGDX, it seems that you are just started. Because when I was just started I was asking the same questions :)
这里有一个提示:
Here is a tip:
如果您的游戏中发生碰撞,并且如果用户通过拖动窗口等方式使游戏进入休眠状态.Gdx.graphics.getDeltaTime()
函数将返回一个意外的高值.例如 2 秒而不是您期望的 16 毫秒 (60fps).在这种情况下,您的角色会跳跃,穿过墙壁并且碰撞检测将失败.所以这是你可以做的:
If you have collision in your game, and if the user sleeps the game by dragging the window etc. Gdx.graphics.getDeltaTime()
function will return an unexpectedly high value. For example 2 seconds instead of 16 milliseconds (60fps) you were expecting. And in that case your character will jump, through walls and collision detection will fail. So here is what you can do:
- 您可以覆盖该函数并取最后 10 帧的平均值并返回该值(在某些情况下有点缺陷,但在其他情况下会更好).
- 如果值超过 30 秒,则可以返回 30 毫秒.所以角色不会跳,穿墙……即使玩家睡了一个世纪.
- 将两者结合起来.(最佳)取平均值时不要计算超过 30 毫秒的值.
<小时>
我写了一个关于这个主题的页面,它在我的网站教程部分.看看这个.nightlybuilddigital.ga
相关文章