好的,所以我看了无数线程试图找到解决方案,但还没找到。在我尝试使用Java进行的3场比赛中,我偶尔会遇到延迟/屏幕撕裂。当它发生时,我的FPS似乎是相同的,但它仍然滞后。这是我正在使用的游戏循环和渲染方法。
public void run() {
long lastTime = System.nanoTime();
double nsPerTick = 1000000000D / UPS;
int frames = 0;
int updates = 0;
long lastTimer = System.currentTimeMillis();
double delta = 0;
while (running) {
long now = System.nanoTime();
delta += (now - lastTime) / nsPerTick;
lastTime = now;
while (delta >= 1) {
update();
updates++;
delta--;
render();
frames++;
}
if (System.currentTimeMillis() - lastTimer >= 1000) {
lastTimer += 1000;
System.out.println("Frames: " + frames + " | Updates: " + updates);
frames = 0;
updates = 0;
}
}
}
public void render() {
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
this.createBufferStrategy(3);
return;
}
do {
do {
Graphics g = bs.getDrawGraphics();
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(canvas, 0, 0, screenSize.width + 10, screenSize.height + 10, null);
g2.setColor(Color.WHITE);
g2.fillRect(0, 0, screenSize.width, screenSize.height);
if (getCurrentScreen() != null) {
getCurrentScreen().render(g2);
}
Toolkit.getDefaultToolkit().sync();
g.dispose();
} while (bs.contentsRestored());
bs.show();
} while (bs.contentsLost());
}
有人对此有任何解决方案吗?谢谢!