2013-03-23 51 views
5

我最近開始使用XNA遊戲工作室4.0製作視頻遊戲。我使用按鈕列表製作了一個包含4個精靈字體的主菜單。當我按下向上和向下箭頭時,他們將顏色從白色變爲黃色。主菜單導航/鍵盤輸入速度太快

我的問題是,當我滾動時,它從頂部字體到底部字體非常快,直接進入最後一個字體。我不確定這是爲什麼?是因爲我把它放在更新方法中,並且每隔60秒左右調用一次?

這是我的代碼,當我按箭頭鍵。

public void Update(GameTime gameTime) 
    { 
     keyboard = Keyboard.GetState(); 

     if (CheckKeyboard(Keys.Up)) 
     { 
      if (selected > 0) 
      { 
       selected--; 
      } 
     } 
     if (CheckKeyboard(Keys.Down)) 
     { 
      if (selected < buttonList.Count - 1) 
      { 
       selected++; 
      } 
     } 

     keyboard = prevKeyboard; 
    } 

    public bool CheckKeyboard(Keys key) 
    { 
     return (keyboard.IsKeyDown(key) && prevKeyboard.IsKeyUp(key)); 
    } 

我需要一個人來幫助我減慢到合理的速度。

如果你能幫助我,將不勝感激。

+0

很高興看到新用戶的SO。只是你知道,我們更喜歡你不使用謝謝你。 – yumaikas 2013-03-23 04:53:43

回答

1

我認爲這個問題是因爲你沒有正確設置prevKeyboard

試試這個:

public void Update(GameTime gameTime) 
{ 
    keyboard = Keyboard.GetState(); 

    if (CheckKeyboard(Keys.Up)) 
    { 
     if (selected > 0) 
     { 
      selected--; 
     } 
    } 
    if (CheckKeyboard(Keys.Down)) 
    { 
     if (selected < buttonList.Count - 1) 
     { 
      selected++; 
     } 
    } 

    prevKeyboard = keyboard; // <=========== CHANGE MADE HERE 
} 

public bool CheckKeyboard(Keys key) 
{ 
    return (keyboard.IsKeyDown(key) && prevKeyboard.IsKeyUp(key)); 
} 
+0

它的工作。謝謝你的協助。 – hunteroatway17 2013-03-23 14:33:00

+0

@ hunteroatway17沒問題。不要忘記標記正確答案,以便整個SO社區都能受益。 – rhughes 2013-03-23 14:40:07

-1

我認爲它是因爲

if (CheckKeyboard(Keys.Up)) 
    { 
     if (selected > 0) 
     { 
      selected--; 
      // This loops executes so quick before you release button. 
      // Make changes here to stop the loop if the button is pressed and loop 
      // executed once.(May be) just **return;** would do ? 
     } 
    } 
    if (CheckKeyboard(Keys.Down)) 
    { 
     if (selected < buttonList.Count - 1) 
     { 
      selected++; 
      // This loops executes so quick before you release button. 
      // Make changes here to stop the loop if the button is pressed and loop 
      // executed once.(May be) just **return;** would do ? 
     } 
    }