2011-11-25 23 views
0

我在做一個小遊戲在Processing這是類似於吉他英雄風格的遊戲,我試圖做兩件事情:處理P5 - 計時器問題與米利斯()

  1. 當遊戲加載,停止移動
  2. 在遊戲過程中的時間,允許暫停功能

現在,我知道我不能停止,因爲米利斯(時間)返回毫秒,因爲應用程序啓動,所以我的計時器將需要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」,並songNotesNote類型的對象的數組,我寫自己。它有2個成員變量,noteToBePlayedtimeToBePlayed。方法getStartTime()返回timeToBePlayed,這是一個毫秒值。

任何幫助表示讚賞。 謝謝

回答

3

如何讓另一個整數存儲時間,當你暫停和使用它來抵消遊戲計時器?

所以,在「gameInProgress」模式更新currentTimerValuepreviousTimerValue和「暫停/菜單」模式,您更新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")); 
} 

單擊草圖切換模式,並期待在控制檯倍。你會注意到你只能在切換之間鬆開幾毫秒,這是可以接受的。

+0

哇,我知道這很簡單!我想我需要一個額外的數字值來跟蹤偏移量,但不能把我的頭圍繞它。謝謝。 – eoinzy

0

不使用系統計時器,但使用具有暫停功能的特殊計時器類。我確信你自己實現這樣的課程並不難。我知道java有Timer類,但不幸的是它不支持暫停功能。