簡體   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