游戏状态导致我的游戏出错

时间:2013-12-08 23:57:05

标签: c# debugging xna

所以我在我的游戏中使用Gamestates,当我到达游戏或赢得屏幕时我遇到了错误。我试图对其进行编码,以便当您按 M 时,它会返回到原始主菜单,以便您可以重新开始播放。
然而,在按 M 时,游戏似乎有bug并且主菜单不断闪烁,当它停止时,你点击播放,它会带你回到gameover或win屏幕!我试过调试但是无济于事。我将尝试在下面添加相关代码。

更新:

switch (CurrentGameState)
{
    case GameState.MainMenu:
        if (btnPlay.isClicked == true) CurrentGameState = GameState.Playing;
        btnPlay.Update(mouse);

        if (btnInstructions.isClicked == true) CurrentGameState = GameState.Instructions;
        btnInstructions.Update(mouse);
        break;

    case GameState.Instructions:
        if (instrPlay.isClicked == true) CurrentGameState = GameState.Playing;
        instrPlay.Update(mouse);
        break;

    case GameState.Playing:       
        //functions for game playing here

        #region game over
        if (hastouched2 > 0 && Ball.ballPos.Y > 740 || hastouched3 > 0 && Ball.ballPos.Y > 740 || Box == null && Ball.ballPos.Y > 740)
            CurrentGameState = GameState.Gameover;

        #endregion
        break;

    case GameState.Gameover:
        if (Keyboard.GetState().IsKeyDown(Keys.M))  
            CurrentGameState = GameState.MainMenu;            
        break;

    case GameState.YouWin:
        if (Keyboard.GetState().IsKeyDown(Keys.M))
            CurrentGameState = GameState.MainMenu;
        break;
}

base.Update(gameTime);

平局:

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.Black);

    spriteBatch.Begin();
    switch (CurrentGameState)
    {
        case GameState.MainMenu:
            spriteBatch.Draw(Content.Load<Texture2D>("mainMenu"), new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
            btnPlay.Draw(spriteBatch);
            btnInstructions.Draw(spriteBatch);
            break;

        case GameState.Instructions:
            spriteBatch.Draw(Content.Load<Texture2D>("instructions_screen"), new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
            instrPlay.Draw(spriteBatch);
            break;

        case GameState.Playing:
            spriteBatch.Draw(background, bgPos, Color.White);
            if (Box != null)
            {
                Box.Draw(spriteBatch);
            }

            if (Box2 != null)
            {
                Box2.Draw(spriteBatch);
            }

            if (Box3 != null)
            {
                Box3.Draw(spriteBatch);
            }
            break;

        case GameState.Gameover:
            spriteBatch.Draw(Content.Load<Texture2D>("gameover"), new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
            break;

        case GameState.YouWin:
            spriteBatch.Draw(Content.Load<Texture2D>("youwin"), new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
            break;

谢谢, 富

1 个答案:

答案 0 :(得分:0)

这件事

if (hastouched2 > 0 && Ball.ballPos.Y > 740 || hastouched3 > 0 && Ball.ballPos.Y > 740 || Box == null && Ball.ballPos.Y > 740)

触发了游戏。

但是当你设置GameState.Playing时,你应该重置每个变量和Ball.ballPos,否则你将在游戏状态恢复。同样的事情发生在GameState.YouWin