0

我的所有三个屏幕/状态都可以正常工作,但是,我实现了第四个作为信息屏幕。到目前为止还不错,但是当我运行游戏并按“H”键时,它不会将屏幕更改为另一个背景(到目前为止我所做的)。下面是代码:

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”兼容?谢谢你。

4

1 回答 1

1

H密钥将不起作用,因为您的更新代码HUpdateInformation...

它的实际作用是:如果您在信息屏幕中,请按 H 转到信息屏幕(这没有意义)

您应该将H检测代码移动到更合适的位置。你在哪里S检测码在哪里?

另外,我建议您在游戏状态中使用枚举而不是数字。

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

这样,维护和理解起来就容易多了。(见下面的例子)

switch(GameState)
{
    case gameStates.Started:
         //Do something
         break;
}
于 2012-04-19T17:18:25.113 回答