【发布时间】:2018-01-23 22:02:05
【问题描述】:
我目前正在开发一个基于 Java 2D 的游戏,这是我目前基于教程的游戏循环:
public class FixedTSGameLoop implements Runnable
{
private MapPanel _gamePanel;
public FixedTSGameLoop(MapPanel panel)
{
this._gamePanel = panel;
}
@Override
public void run()
{
long lastTime = System.nanoTime(), now;
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int updates = 0;
int frames = 0;
while (this._gamePanel.isRunning())
{
now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while (delta >= 1)
{
tick();
updates++;
delta--;
}
render();
frames++;
if (System.currentTimeMillis() - timer > 1000)
{
timer += 1000;
System.out.println("FPS: " + frames + " TICKS: " + updates);
frames = 0;
updates = 0;
}
}
}
private void tick()
{
/**
* Logic goes here:
*/
this._gamePanel.setLogic();
}
private void render()
{
/**
* Rendering the map panel
*/
this._gamePanel.repaint();
}
}
现在,我想为帧速率设置一个上限。我该如何以最有效的方式做到这一点? *任何有关游戏循环本身的一般提示将不胜感激! 谢谢!
【问题讨论】:
-
你不是已经有 60fps 的上限了吗?
-
“滴答”设置为每秒调用 60 次。渲染速率目前没有上限/设置为某个值。
标签: java frame-rate java-2d game-loop