2013-10-15 68 views
0

我試圖讓遊戲每0.8秒改變一次鼴鼠的波動。 (我的遊戲是簡單的「捶痣,對練)Slick2D每0.8渲染

我的代碼來改變波:

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,ev一切都很好。波浪每隔1秒改變一次。
我正在使用0.8,因爲我試圖實施一個難度選擇(簡單,中等,難...)越難,波浪變化越快。

上面的代碼是我的問題的罪魁禍首?如果是這樣,請爲我解決這個問題。

回答

1

我們沒有看到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秒。

+0

感謝您的解釋! – junyi00