在libgdx中暂停TimeUtils
问题描述:
在我的游戏中,我有0.45s基础上定期产生的对象。当它们进入这个世界时,他们会在那个时候获得一个nanoTime的副本。这样,我可以检查当前的nanoTime以在经过一定时间后移除对象。在libgdx中暂停TimeUtils
现在的问题是当你暂停游戏时。因为当你离开暂停屏幕后(如果暂停屏幕长于删除对象所用的时间),nanoTime将继续进行,这将使对象直接消失。
有没有一种方法,当你进入暂停画面,并从当您退出所以你走出暂停屏幕后的对象不会直接消失在那里留下继续冻结nanoTime或类似的东西?
答
我会建议改变你存储产卵物体的方式。而不是储存他们被添加到世界上的时间,你应该存储他们离开的时间有多长。
在每个渲染帧上,libGDX提供了一个delta
参数来衡量自上次渲染时间以来已经过了多长时间。如果游戏未暂停,您将从timeLeftToLive
变量(或其他名称)这个delta
值减少。
下面的代码说明了我的观点(仅仅是一个虚拟类):
class ObjectTimer {
private float timeToLive = 0; //How long left in seconds
public ObjectTimer(float timeToLive) {
this.timeToLive = timeToLive;
}
//This method would be called on every render
//if the game isn't currently paused
public void update() {
//Decrease time left by however much time it has lived for
this.timeToLive -= Gdx.graphics.getDeltaTime();
}
//Return if all the time has elapsed yet
//when true you can de-spawn the object
public boolean isFinished() {
return timeToLive <= 0;
}
}
那么你可能在你的游戏主循环,会做线沿线的更新的东西:
//Do this if the game isn't paused
if (!paused) {
//For each of your sprites
for (MySprite sprite: spriteArray) {
//Update the time run for
sprite.getObjectTimer().update();
//Check if the sprite has used all its time
if (sprite.getObjectTimer().isFinished()) {
//Despawning code...
}
}
}
是的,我已经改变了我的对象来使用delta时间而不是nanoTime,现在它可以正常工作。 –