【问题标题】:XNA Game loop errorXNA 游戏循环错误
【发布时间】:2015-06-03 11:23:29
【问题描述】:

我一直在开发我的第一个 Xna 游戏,是的,这就是我编码不佳的借口。我已经设法解决了大多数问题,但不是这个。我实在看不下去了。当我的玩家死亡(称为精灵)时,我试图让游戏恢复默认状态。你们能帮忙吗?需要明确的是,当我的玩家死亡时,我希望 GameState 回到 MainMenu,并且按钮恢复正常。现在,当玩家死亡时,无法按下按钮。提前致谢。 我将提供与菜单/按钮有关的所有内容。 在游戏 1 中:

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);
    }
}

您有什么建议吗? 我不能玩两次,我的按钮也不能用两次。 最好的问候。

【问题讨论】:

  • cButtonplay 和 cButtonHS 是您自己的课程吗?您可以为他们粘贴代码吗?另外,按钮 .isClicked 属性在哪里设置为 true?
  • 嗯,很难理解提供的代码的问题...我知道健康条是玩家健康条的集合?如果是这样,则在您的更新方法中,“IsSpriteAlive”(这是玩家吗?)应设置为一个值,具体取决于所有生命条是否为 0。有了这个设置,如果他不活着,那么只需更改游戏状态值。

标签: c# xna


【解决方案1】:

您绝对应该考虑使用 GameComponents 而不是使用 switch 语句。不那么混乱,您可以将每个屏幕的关注点分开,因为功能保存在每个屏幕类中。 Here is the documentation.

从页面:

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

然后,您可以通过覆盖 GameComponent.Update、DrawableGameComponent.Draw 和 GameComponent.Initialize 将游戏逻辑和渲染代码添加到游戏组件。游戏组件通过将组件传递给 Game.Components.Add 来向游戏注册。

注册的组件将拥有从 Game.Initialize、Game.Update 和 Game.Draw 方法调用的 draw、update 和 initialize 方法。

【讨论】:

  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
  • 啊,谢谢提醒,我会编辑问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-15
  • 2014-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多