获取变量以正确递增的问题

时间:2015-03-11 12:32:25

标签: c# xna

我正在为我的最后一年项目制作一个教育游戏,当我的玩家盒子与另一个物品盒子发生碰撞时,在增加值时会遇到一些错误。当我的玩家已经死亡然后重置时,错误就会出现,第一项收集得很好但是程序似乎跳过下一个if语句并抽出第三项而不是第二项。

我添加了我认为问题所在的代码。

LevelManager.cs

                for (int x = hints.Count - 1; x >= 0; x--)
            {
                hints[x].Update(gameTime);
                if (player.CollisionRectangle.Intersects(
                    hints[x].CollisionRectangle))
                {
                    hints.RemoveAt(x);
                    player.Hint++;
                }
            }

Game1.cs

 if ((gameState == GameState.Playing) ||
            (gameState == GameState.PlayerDead) ||
            (gameState == GameState.GameOver))
        {
            spriteBatch.Begin(
          SpriteSortMode.BackToFront,
          BlendState.AlphaBlend);

            TileMap.Draw(spriteBatch);
            player.Draw(spriteBatch);
            LevelManager.Draw(spriteBatch);
            spriteBatch.DrawString(
                pericles8,
                "Score: " + player.Score.ToString(),
                scorePosition,
                Color.White);
            spriteBatch.DrawString(
                pericles8,
                "Lives Remaining: " + player.LivesRemaining.ToString(),
                livesPosition,
                Color.White);

            if (player.Hint == 1)
            {
                spriteBatch.DrawString(
                    pericles8,
                    "Hint: " + "Antivirus is needed to protect your PC",
                    HintLocation,
                    Color.Black);
            }

            if (player.Hint == 2)
            {
                spriteBatch.DrawString(
                    pericles8,
                    "Hint: " + "When the first virus was created, it changed the name of floppy disks that were infected",
                    HintLocation,
                    Color.Black);
            }

            if (player.Hint == 3)
            {
                spriteBatch.DrawString(
                    pericles8,
                    "Hint: " + "Blue Whale is NOT the name of a virus and also whilst adware is annoying, it is not considered a virus",
                    HintLocation,
                    Color.Black);
            }

            if (player.Hint == 4)
            {
                spriteBatch.DrawString(
                    pericles8,
                    "Hint: " + "By having a firewall up, you lock all backdoors into your PC." + "\n" + "Also by staying clear of sites you dont know, you lower the chance of viruses getting into your PC" + "\n"
                    + "However you should install relevant protection to increase your chance of not getting viruses",
                    HintLocation,
                    Color.Black);
            }
            spriteBatch.End();
        }

        if (gameState == GameState.PlayerDead)
        {
            player.Hint = 0;
        }

非常感谢任何可以给予的帮助 干杯

1 个答案:

答案 0 :(得分:0)

我没有看到您提供的代码段中的错误。我可以说你可以在线上设置断点

player.Hint++;

(或者在Hint属性的setter中更好的事件)并查看它何时以及为何变为3。