2012-04-19 74 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; 
}