我正在使用 libgdx 和 android studio。这里我试图在一定的时间间隔(0.5秒)后调用一个方法(用于创建纹理)。但是当我运行程序时,它显示“没有响应”对话框。
private float x=System.currentTimeMillis();
//in update method
public void update(float dt) {
handleinput();
while(x<=System.currentTimeMillis()){
eggs.create();
//eggs.update(dt);
x+=500;
}
}
要做什么?
最佳答案
要处理时间,请使用每次调用渲染时添加增量时间的变量,当该变量优于 1.0f 时意味着已经过去了一秒,您的代码将如下所示:
private float timeSeconds = 0f;
private float period = 1f;
public void render() {
//Execute handleEvent each 1 second
timeSeconds +=Gdx.graphics.getDeltaTime();
if(timeSeconds > period){
timeSeconds-=period;
handleEvent();
}
[...]
}
public void handleEvent() {
[...]
}
为了保持组织性,我个人在我的主游戏类上有一个数组,用于保存所有定时事件并处理渲染周期中的所有内容。
关于java - 如何在一定的时间间隔后重复调用一个方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44781728/