2013-10-15 67 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