Java Libgdx 如何计算经过的时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23932887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 09:11:37  来源:igfitidea点击:

Libgdx how to calculate elapsed time

javatimerlibgdx

提问by Andrew

I was trying to calculate elapsed time in libgdx by adding in the render method the value of delta to my float value which is the time i measure since play state starts. The bug or problem, how you want to call it, is that by my calculations, the time displayed doing this is two times the real time. I tried to divide by 2 and I was pretty close to the real time, which means that by adding delta every render call I don't get real time in seconds. Why is this happening?

我试图通过在渲染方法中将 delta 的值添加到我的浮点值来计算 libgdx 中的经过时间,这是我自播放状态开始以来测量的时间。错误或问题,您想如何称呼它,是根据我的计算,显示的时间是实际时间的两倍。我试图除以 2 并且我非常接近实时,这意味着通过添加每次渲染调用的增量,我不会在几秒钟内获得实时。为什么会这样?

private float time=0;
public void render () {
    time +=Gdx.graphics.getDeltaTime();
}

the above doesn't get the real time and my question is why? EDIT: i am using a screen but it doesn't matter i tried both Gdx.graphics.getDeltaTime() and delta argument from render method.

以上没有得到实时,我的问题是为什么?编辑:我正在使用屏幕,但没关系我尝试了 Gdx.graphics.getDeltaTime() 和 render 方法中的 delta 参数。

采纳答案by Christian Tucker

LibGDX is still Java at heart, just use the standard methods for getting the time. At the start of the application use

LibGDX 本质上仍然是 Java,只是使用标准方法来获取时间。在应用程序开始时使用

startTime = System.currentTimeMillis();

then in the render you can do

然后在渲染中你可以做

System.out.println("Time elapsed in seconds = " + ((System.currentTimeMillis() - startTime) / 1000));

Make sure startTime is declared as a long.

确保将 startTime 声明为 long。

As to why it's not working with the method you posted, I'm not sure.

至于为什么它不适用于您发布的方法,我不确定。

EDIT: If your LibGDX project isn't set to run at a fixed rate, then the frame-rate change will cause a change in Gdx.graphics.getDeltaTime(), because the delta time is returned for the last frame updated.

编辑:如果您的 LibGDX 项目未设置为以固定速率运行,则帧速率更改将导致 Gdx.graphics.getDeltaTime() 发生更改,因为为最后更新的帧返回增量时间。

回答by galmosh

Why not just take a system time stamp from standard java libraries. Do that when you start the application and when you want to calculate the elapsed time reduce that initial value from the current time.

为什么不从标准 Java 库中获取系统时间戳。当您启动应用程序以及想要计算经过的时间时,请从当前时间减少初始值。

long startTime = System.currentTimeMillis();

and then whenever you want to get the elapsed time do:

然后每当你想获得经过的时间时,请执行以下操作:

long elapsed time = System.currentTimeMillis() - startTime;

I don't think adding the delta is very accurate since probably some of the time is lost between when libgdx stops measuring the delta and when it starts again in which case it's going to drift over time.

我不认为添加增量是非常准确的,因为在 libgdx 停止测量增量和再次开始测量增量之间可能会丢失一些时间,在这种情况下它会随着时间的推移而漂移。

Not sure why you get about twice as fast though... I know the render call is called 60 times a second. maybe the delta is somehow considering 30 frames per second...

不知道为什么你的速度会快两倍......我知道渲染调用每秒调用 60 次。也许增量以某种方式考虑每秒 30 帧......

BTW, there's also getRawDeltaTime() This should give you something more accurate.

顺便说一句,还有 getRawDeltaTime() 这应该给你更准确的东西。

Update: As Christian commented - you need to divide by 1000 to get to seconds resolution rather than milliseconds.

更新:正如 Christian 评论的那样-您需要除以 1000 才能获得秒分辨率而不是毫秒。

回答by Tomasz Pos?uszny

You can use wrapper TimeUtils.

您可以使用包装器 TimeUtils。

Get current time:

获取当前时间:

long startTime = TimeUtils.millis();

Get time elapsed since startTime:

获取自 startTime 以来经过的时间:

long elapsedTime = TimeUtils.timeSinceMillis(startTime);

回答by user7830958

TimeUtils is required for portability. Avoid using system classes if you are planning on deploying on multiple platforms. LibGdx features its own array class for this reason as well.

TimeUtils 是可移植性所必需的。如果您计划在多个平台上部署,请避免使用系统类。出于这个原因,LibGdx 也有自己的数组类。

Example:

例子:

public long time;
time = TimeUtils.nanoTime();

From the wiki:

来自维基:

Class TimeUtils "Wrapper around System.nanoTime() and System.currentTimeMillis(). Use this if you want to be compatible across all platforms!"

TimeUtils 类“围绕 System.nanoTime() 和 System.currentTimeMillis() 进行包装。如果您想在所有平台上兼容,请使用它!”