我正在处理中制作一个类似于吉他英雄风格游戏的小游戏,我正在尝试做两件事:
- 当游戏加载时,停止移动时间
- 在游戏过程中,允许暂停功能
现在,我知道我不能停止时间,因为 millis() 返回应用程序启动后的毫秒数,所以我的计时器需要millis() - millis()
在开始时等于零,所以当用户按下 START 时,他们显然可以从开始. 游戏在开始时读取一个文件,类似于字幕文件,其中包含要播放的音符以及它应该出现在屏幕上的时间(以毫秒为单位)。
我的问题是,当我暂停游戏时,计时器会继续运行,而当我取消暂停游戏时,由于我的逻辑,所有笔记都会“堆积”起来,正如您从我的代码中看到的那样。
有人可以提出比我使用的更好的算法吗?已经很晚了,我整天都在为此工作。我认为问题出在for()
以下几点:
public void draw()
{
if (gameInProgress)
{
currentTimerValue = millis(); // Update the timer with the current milliseconds
// Check to see if the note times falls between the current time, or since the last loop (difficult to match exact millisecond)
for(int i=0 ; i<songNotes.length ; i++)
{
if( songNotes[i].getStartTime() > previousTimerValue && songNotes[i].getStartTime() <=currentTimerValue)
notes.add(songNotes[i]);
}
noStroke();
textFont(f,18);
drawButtons(); //Draws coloured buttons relating to Button presses on the controller
drawHighScoreBox(); // Draws high score box up top right
drawLines(); // Draws the strings
moveNotes(); // Moves the notes across from right to left
//Now set the cutoff for oldest note to display
previousTimerValue=currentTimerValue; //Used everytime on the following loop
}
else
{
drawMenu(); // Draw the Main/Pause menu
}
}
gameInProgress
注意:当用户按下暂停按钮时,布尔值在下面设置,例如“P”,并且是我自己编写songNotes
的类型对象数组。Note
它有 2 个成员变量noteToBePlayed
和timeToBePlayed
. 该方法getStartTime()
返回timeToBePlayed
毫秒值。
任何帮助表示赞赏。谢谢