如何在libgdx中添加时间事件

Dea*_*eus 2 java android libgdx

我想知道如何在libgdx中为我的事件添加时间.我有一个按钮,按下它时会出现一个精灵.我希望精灵只出现一小段时间.我怎样才能做到这一点?我使用Scene2D将sprite作为演员.

我将在伪代码中向您展示一个示例.

wait time = 5 second;
current time = get time;
if (current time > wait time) {
    // do the following
}
Run Code Online (Sandbox Code Playgroud)

tob*_*oef 8

有两种方法可以做到这一点.您可以使用类似于伪代码的东西,也可以使用计时器.

手动计算:

private Long lifeTime;
private Long delay = 2000L; //1000 milliseconds per second, so 2 seconds.

public void create () {
    lifeTime = System.currentTimeMillis();
}

public void render () {
    lifeTime += Gdx.graphics.getDeltaTime();
    if (lifetime > delay) {
        //Do something
    }
}
Run Code Online (Sandbox Code Playgroud)

使用计时器:

private float delay = 2; //In seconds this time

//At some point you set the timer
Timer.schedule(new Task(){
    @Override
    public void run() {
        // Do something
    }
}, delay);
Run Code Online (Sandbox Code Playgroud)

在此处阅读更多内容:https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/utils/Timer.html