簡體   English   中英

每0.8進行Slick2D渲染

[英]Slick2D rendering every 0.8

我試圖使游戲每0.8秒更改一次痣的波動。 (我的游戲就是“ Whack The Moles,練習)。

我改變浪潮的代碼:

double TIME = 0.8;

if (Next) { //If 0.8 seconds is up
        if (Over == false) { //make sure the game hasn't ended
            start = (int) (cTime/1000); //cTime is the milisecond since game started
            String wave = MolesWaves.RandomWave(); //getting the new wave data 
            initWave(wave); 
            Next = false; //disallow the game from changing wave
        }
    }
    else {
        if (((cTime/1000) - start) >= TIME) { //changing speed
            System.out.println("Test: " + ((cTime/1000)-start));
            Next = true; //allow game to change waves
        }
    }

System.out.println("Test: " + ((cTime/1000)-start)); ,這就是我從輸出日志中得到的。

Test: 0.802
Test: 0.817
Test: 0.833
Test: 0.852
Test: 0.867
Test: 0.883
Test: 0.9
Test: 0.917
Test: 0.933
Test: 0.95
Test: 0.967
Test: 0.983
Test: 1.0

問題在於,波浪每秒變化13次,一旦達到每秒一段時間,它將停止切換,然后再次啟動。
如果TIME值為1 ,則一切正常。 波浪每1秒變化一次。
我正在使用0.8,因為我正在嘗試實現難度選擇(容易,中等,困難...),難度越大,波動變化就越快。

代碼是造成我問題的元凶嗎? 如果是這樣,請為我解決。

我們看不到start的類型,但我假設它是double 如果是這樣,罪魁禍首是這一行:

start = (int) (cTime/1000); //cTime is the milisecond since game started

想象cTime是900,最后一個波浪在時間0開始(因此應該開始一個新波浪)。 然后當新的一波開始時,您將設置start = (int)(900/1000); 這是一個截斷的整數除法,因此start的新值為0 但這與舊值相同-因此,由於什么都沒有改變,因此下次檢查時間條件時,新波形將立即再次開始。

而不是進行整數除法,而是將整數cTime轉換為double精度並在浮點數中執行除法和比較:

start = ((double) cTime) / 1000.0;
// ...
if ((((double)cTime/1000.0) - start) >= TIME) { //changing speed

然后,上述方案中start的新值應為0.9 ,並且新回合應允許持續0.8秒。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM