XNA游戏循环错误

时间:2015-06-03 11:23:30

标签: c# xna

我一直在研究我的第一款Xna游戏,是的,这是我编写糟糕编码的借口。我设法解决了大多数问题,但不是这个问题。我无法真正理解这一点。当我的玩家死亡时(我称之为精灵),我正试图让游戏恢复默认状态。你们能帮忙吗?要清楚,当我的玩家死亡时,我希望GameState回到MainMenu,然后按钮恢复正常。现在,当玩家死亡时,无法按下按钮。提前致谢。 我将提供与菜单/按钮有关的所有内容。 在Game1中:

enum GameState
    {
        MainMenu, 
        Play,
        HighScore,
        GameOver,
    }
    GameState CurrentGameState = GameState.MainMenu;

``

cButtonplay btnPlay;
    cButtonHS btnHighScore;
protected override void LoadContent()
    {
        IsMouseVisible = true;
        btnPlay = new cButtonplay(Content.Load<Texture2D>("button_play"), graphics.GraphicsDevice);
        btnPlay.setPosition(new Vector2(300, 250));

        btnHighScore = new cButtonHS(Content.Load<Texture2D>("button_highscore"), graphics.GraphicsDevice);
        btnHighScore.setPosition(new Vector2(300, 300));
    }

我的更新逻辑:

protected override void Update(GameTime gameTime)
    {

      //MENU
        isSpriteAlive = true;
        MouseState mouse = Mouse.GetState();
        switch (CurrentGameState)
        {
            case GameState.MainMenu:

                if (btnPlay.isClicked == true)
                {
                    btnPlay.isClicked = false;
                    CurrentGameState = GameState.Play;
                }
                btnPlay.Update(mouse);
                if (btnHighScore.isClicked == true)
                {
                    btnHighScore.isClicked = false;
                    CurrentGameState = GameState.HighScore;
                   // btnHighScore.Update(mouse);
                }

                break;
                case GameState.Play:

                isSpriteAlive = true;
                // Here is alot of various code
                case GameState.HighScore:
                if (btnHighScore.isClicked == true) CurrentGameState =                        GameState.HighScore;
                btnHighScore.Update(mouse);
                break;

            case GameState.GameOver:
                btnHighScore.isClicked = false;
                btnHighScore.Update(mouse);
                break;

最后我的绘制方法:

protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.ForestGreen);
        spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null, null);

        //MENU
        switch (CurrentGameState)
        {
            case GameState.MainMenu:
                spriteBatch.Draw(Content.Load<Texture2D>("menu_background"), new Rectangle(0, 0, screenWidth, screenHeight), Color.WhiteSmoke);
                btnPlay.playDraw(spriteBatch);
                btnHighScore.highScoreDraw(spriteBatch);
                printText.PrintMainMenu("By: Felix Claesson\nand Carl Wikstrom", spriteBatch, 250, 500);
                //printText.Print("$ " + points * 25, spriteBatch, 0, 0);

                break;

            case GameState.Play:
                if (isSpriteAlive == true)
                {
                    spriteBatch.Draw(backgroundTexture, backgroundPosition, Color.White);
                    foreach (Enemies enemy in enemies)
                        enemy.Draw(spriteBatch);
                    foreach (Bullets bullet in bullets)
                        bullet.Draw(spriteBatch);
                    foreach (healthBars healthbar in healthbars)
                        healthbar.Draw(spriteBatch);
                    if (isSpriteAlive == true)
                        spriteBatch.Draw(spriteTexture, spritePosition, new Rectangle(56 * frames, 0, 56, 51), Color.White, playerRotation, spriteOrigin, 1f, SpriteEffects.None, 0);
                    printText.Print("$ " + points * 25, spriteBatch, 0, 0);
                }
                else CurrentGameState = GameState.GameOver;
                break;

            case GameState.HighScore:
                printText.Print(":-)", spriteBatch, 350, 300);
                break;

            case GameState.GameOver:
                printText.Print("h", spriteBatch, 350, 300);
                btnHighScore.highScoreDraw(spriteBatch);
                break;
        }
        spriteBatch.End();
        base.Draw(gameTime);
    }
}

你有任何提示吗? 我不能玩两次,我的按钮不能工作两次。 最好的问候。

1 个答案:

答案 0 :(得分:0)

你一定要考虑使用GameComponents而不是使用switch语句。不那么混乱,您可以分离每个屏幕的关注点,因为功能保存在每个屏幕类中。 Here is the documentation.

从页面:

  

游戏组件提供了一种向游戏添加功能的模块化方式。您可以通过从GameComponent类派生新组件来创建游戏组件,或者,如果组件从DrawableGameComponent类加载和绘制图形内容,则创建游戏组件。

     

然后通过重写GameComponent.Update,DrawableGameComponent.Draw和GameComponent.Initialize,将游戏逻辑和渲染代码添加到游戏组件中。通过将组件传递给Game.Components.Add。

,游戏组件在游戏中注册      

注册组件将具有从Game.Initialize,Game.Update和Game.Draw方法调用的绘制,更新和初始化方法。