2013-06-20 45 views
1

當按下某個鍵時,屏幕上顯示了一個紋理。對此的檢查位於基於布爾help_on的更新方法中。按F1即可,如果help_on爲false,則顯示紋理並使help_on爲true。如果help_on爲true,那麼將其設爲false,這裏應該刪除一個精靈。任何人都可以給我一個小費嗎?我已經知道這可能不是做這件事的方法,但我不知道如何以另一種方式做。XNA正在刪除Sprite

回答

0

你需要做兩件事情,使這項工作,你打算:

首先,你需要存儲整個幀的鍵盤狀態,所以你可以檢查當狀態變化。所以:當一個鍵在一幀上「上」,然後在下一個「下」時,你知道該鍵被「按下」了該幀。

,切換可變,只需設置值變量爲「not」的變量的當前(其設置爲它的倒數)。

請看下面的代碼,它可以把你的遊戲類:

bool help_on; 
KeyboardState lastKeyboardState; 

protected override void Update(GameTime gameTime) 
{ 
    KeyboardState keyboardState = Keyboard.GetState(); 

    // If the F1 key went down on this frame 
    if(keyboardState.IsKeyDown(Keys.F1) && lastKeyboardState.IsKeyUp(Keys.F1)) 
    { 
     help_on = !help_on; // Toggle the help_on variable 
    } 

    lastKeyboardState = keyboardState; 

    base.Update(gameTime); 
} 

protected override void Draw(GameTime gameTime) 
{ 
    if(help_on) 
    { 
     // Draw your help screen here 
    } 

    base.Draw(gameTime); 
} 
+0

我無法描述我對你的感恩回答!非常感謝:) – user2459750

+0

@ user2459750沒問題。不要忘記接受答案:) –

+0

@ user2459750另請注意,我對示例代碼做了一個小而重要的錯誤修復編輯。對於那個很抱歉 :) –

0

因爲我不知道你的遊戲架構做,我會提供一個非常簡單的方法:
在你更新方法:

kbdState = Keyboard.GetState(); 
help_on = kbdState.IsKeyDown(Keys.F1); 

它總是好的緩存KeyboardState因爲你通常希望處理更多的按鍵,而不僅僅是F1。 然後,在你繪製方法:

if (help_on) 
    spriteBatch.Draw(...); //all your drawing code 
+0

謝謝您的時間,但這個也不太工作。解決了。 – user2459750