我试图让游戏每 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次,达到每秒13次就停止切换一段时间,然后又开始。
如果 TIME
的值为 1
,则一切正常。波浪每 1 秒变化一次。
我使用 0.8,因为我试图实现难度选择(简单、中等、困难...)越难,波浪变化越快。
上面的代码是我的问题的罪魁祸首吗?如果是的话,请帮我解决这个问题。
最佳答案
我们没有看到 start
的类型,但我假设它是 double
。如果是这样,罪魁祸首就是这一行:
start = (int) (cTime/1000); //cTime is the milisecond since game started
假设 cTime
为 900,最后一波在时间 0
开始(因此新一波应该开始)。然后,当这个新的 Wave 开始时,您将设置 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 秒。
关于java - Slick2D 每 0.8 渲染一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19381903/