繁体   English   中英

如何在libgdx中每60秒执行一次更新

[英]how to execute an update every 60 seconds in libgdx

我如何在libgdx中每60秒执行一次更新。 我已经尝试过此代码,但实际上“计数器”直接变为0

public void update(float delta){

    stage.act(delta);
    counter-=Gdx.graphics.getRawDeltaTime();;
   if (counter==3)
    {   stage.addActor(oneImg);
    }
    else if(counter==2)
    {
        stage.addActor(twoImg);

    }
    else if(counter==1)
    {   stage.addActor(splashImg);
    }


}

是的,这将会发生。

这是因为libgdx的getRawDelta time方法以浮点数返回值。 当您从柜台中扣除它们时,您可能永远都不会得到完美舍入的数字,例如1、2、3。

因此,仅举一个例子,假设您的计数器是3.29,而getRawDeltaTime返回了0.30。

如果从3.29中扣除,则最终将为2.99,因此您将永远不会碰到if语句。

我这样做的方式是

counter -= Gdx.graphics.getDeltaTime();

if(counter <= 3 && counter > 2) {   
    stage.addActor(oneImg);
} else if(counter <= 2 && counter > 1) {
    stage.addActor(twoImg);
} else if(counter <= 1 && counter > 0)  {
    stage.addActor(splashImg);
}

我希望上述解决方案有意义。

还有一点要指出我留在解决方案中的地方。 每个if条件都将执行多次,而不仅仅是在我的解决方案中执行一次。

这是因为当您说出(counter <= 3 && counter > 2) ,计数器将具有2.9、2.87 ...等值,即直到其在2到3之间为止。要解决此问题,您需要使用一些布尔值。

定义类级别的boolean condition1, condition2, condition3;

修改if语句像

if(counter <= 3 && counter > 2 && !condition1) {   
    stage.addActor(oneImg);
    condition1 = true;
} else if(counter <= 2 && counter > 1 && !condition2) {
    stage.addActor(twoImg);
    condition2 = true;
} else if(counter <= 1 && counter > 0 && !condition3)  {
    stage.addActor(splashImg);
    condition3 = true;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM