0

我正在处理中制作一个类似于吉他英雄风格游戏的小游戏,我正在尝试做两件事:

  1. 当游戏加载时,停止移动时间
  2. 在游戏过程中,允许暂停功能

现在,我知道我不能停止时间,因为 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 个成员变量noteToBePlayedtimeToBePlayed. 该方法getStartTime()返回timeToBePlayed毫秒值。

任何帮助表示赞赏。谢谢

4

2 回答 2

3

当您暂停并使用它来抵消游戏计时器时,有另一个整数来存储时间怎么样?

因此,在“gameInProgress”模式下更新currentTimerValuepreviousTimerValue在“暂停/菜单”模式下更新 a pausedTimerValue,用于抵消“currentTimerValue”。我希望这是有道理的,这听起来更复杂,这就是我的意思:

boolean gameInProgress = true;
int currentTimerValue,previousTimerValue,pausedTimerValue;
void setup(){

}
void draw(){
  if(gameInProgress){
    currentTimerValue = millis()-pausedTimerValue;
    println("currentTimerValue: " + currentTimerValue + " previousTimerValue: " + previousTimerValue);  
    previousTimerValue=currentTimerValue;
  }else{
    pausedTimerValue = millis()-currentTimerValue;
  }
}
void mousePressed(){
  gameInProgress = !gameInProgress;
  println("paused: " + (gameInProgress ? "NO" : "YES"));
}

单击草图以切换模式并在控制台中查看时间。您会注意到在切换之间您只松动了几毫秒,这是可以接受的。

于 2011-11-25T20:26:23.283 回答
0

不使用系统计时器,而是使用具有暂停功能的特殊计时器类。我相信自己实现这样的课程并不难。我知道 java 有 Timer 类,但不幸的是它不支持暂停功能。

于 2011-11-25T16:58:47.917 回答