2012-04-19 97 views
0

我的三個屏幕/狀態都正常工作,但是,我實現了第四個屏幕作爲信息屏幕。到目前爲止,但是當我運行遊戲並按下'H'鍵時,它不會將屏幕更改爲另一個背景(至今我所做的)。下面是代碼:Gamestate運行不正常

public void UpdateInformation(GameTime currentTime) 
{ 
    if (Keyboard.GetState().IsKeyDown(Keys.H)) 
    { 
     GameState = 4; 
    } // GAMESTATE 4 which is the instruction/Information screen. 
} 

這是在更新方法的遊戲狀態代碼:

protected override void Update(GameTime gameTime) 
{ 
    switch (GameState) 
    { 
     case 1: UpdateStarted(gameTime); 
      break; 

     case 2: UpdatePlaying(gameTime); 
      break; 

     case 3: UpdateEnded(gameTime); 
      break; 

     case 4: UpdateInformation(gameTime); 
      break; 
    } 

    base.Update(gameTime); 
} 

我在這裏繪製畫面。

public void DrawInformation(GameTime currentTime) 
{ 
    spriteBatch.Begin(); 
    spriteBatch.Draw(InfoBackground, Vector2.Zero, Color.White); 
    spriteBatch.End(); 
} 

下面是狀態抽獎信息代碼:

protected override void Draw(GameTime gameTime) 
{ 
    switch (GameState) 
    { 
     case 1: DrawStarted(gameTime); 
      break; 

     case 2: DrawPlaying(gameTime); 
      break; 

     case 3: DrawEnded(gameTime); 
      break; 

     case 4: DrawInformation(gameTime); 
      break; 
    } 
} 

我希望這可以幫助,這只是我的H鍵沒有響應,但我的S鍵反應良好,並開始遊戲。四個狀態/屏幕是否與'Gamestate'兼容? 謝謝。

回答

1

H關鍵是行不通的,因爲對於H關鍵你的更新代碼在UpdateInformation ...

其實際作用是:如果你在信息屏幕上的時候,按H去信息屏幕(這沒有意義)

您應該將您的H檢測代碼移到更合適的地方。你的S檢測代碼在哪裏?

此外,我會建議使用枚舉而不是數字爲您的遊戲狀態。

enum gameStates 
{ 
    Started, 
    Playing, 
    Ended, 
    Information, 
} 

這樣,維護和理解起來就容易多了。 (見下例)

switch(GameState) 
{ 
    case gameStates.Started: 
     //Do something 
     break; 
}