Java 我怎样才能每秒做某事?[LibGDX]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21781161/
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
How can I do something every second? [LibGDX]
提问by CodingNub
Lets say I want to make a loop or something that prints out, for example, "Mario" every second. How can I do this? Can't seem to find any good tutorials that teach this anywhere =P
假设我想制作一个循环或打印出来的东西,例如,“马里奥”每秒。我怎样才能做到这一点?似乎在任何地方都找不到任何好的教程来教这个 =P
采纳答案by timeshift117
You can use java.util.Timer.
您可以使用 java.util.Timer。
new Timer().scheduleAtFixedRate(task, after, interval);
task is the method you want to execute, after is the amount of time till the first execution and interval is the time between executions of aforementioned task.
任务是您要执行的方法,之后是第一次执行之前的时间量,间隔是上述任务执行之间的时间。
回答by Springrbua
As @BennX said you can sum up the delta
time you have in your render method or get it by calling Gdx.graphics.getDeltaTime();
. If it is bigger then 1
(delta
is a float
, giving the seconds since the last frame has been drawn), you can execute your task. Instead of reseting your timer by using timer = 0;
you could decrement it by using timer -= 1
, so your tasks get executed more accurate. So if 1 task starts after 1.1 seconds
, cause of a reallybig delta
the next time it gets executed after arround 0.9
seconds.
If you don't like the delta
time solution you can use Libgdx timer, instead of java.util.Timer.
An example of it:
正如@BennX 所说,您可以总结delta
渲染方法中的时间或通过调用Gdx.graphics.getDeltaTime();
. 如果它大于1
(delta
是 a float
,给出自上一帧绘制以来的秒数),您可以执行您的任务。timer = 0;
您可以使用减少计时器,而不是使用重置计时器timer -= 1
,以便您的任务执行得更准确。因此,如果 1 个任务在 之后开始1.1 seconds
,则下一次在大约几秒钟后执行时会导致一个非常大delta
的任务0.9
。如果您不喜欢delta
时间解决方案,您可以使用 Libgdx 计时器,而不是 java.util.Timer。一个例子:
Timer.schedule(new Task(){
@Override
public void run() {
doWhatEverYouWant();
}
}
, delay // (delay)
, amtOfSec // (seconds)
);
This executes the doWhatEverYouWant()
method after a delayof delay
and then every seconds
seconds. You can also give it a 3rd parameter numberOfExecutions
, telling it how often the task should be executed. If you don't give that parameter the task is executed "forever", till it is canceled.
这执行doWhatEverYouWant()
一个方法后延迟的delay
,然后每个和seconds
秒。你也可以给它一个第三个参数numberOfExecutions
,告诉它任务应该多久执行一次。如果您不提供该参数,则任务将“永远”执行,直到被取消。
回答by lxknvlk
I used the TimeUtils of libgdx to change my splashscreen after 1.5 seconds. The code was something like this:
我使用 libgdx 的 TimeUtils 在 1.5 秒后更改我的启动画面。代码是这样的:
initialize:
初始化:
long startTime = 0;
in create method:
在创建方法中:
startTime = TimeUtils.nanoTime();
returns the current value of the system timer, in nanoseconds.
返回系统计时器的当前值,以纳秒为单位。
in update, or render method:
在更新或渲染方法中:
if (TimeUtils.timeSinceNanos(startTime) > 1000000000) {
// if time passed since the time you set startTime at is more than 1 second
//your code here
//also you can set the new startTime
//so this block will execute every one second
startTime = TimeUtils.nanoTime();
}
- check out the libgdx API : TimeUtils
- 查看 libgdx API:TimeUtils
For some reason my solution seemes more elegant to me that those offered here :)
出于某种原因,我的解决方案对我来说似乎比这里提供的更优雅:)