Rogue异常,System.InvalidOperationException,XNA

时间:2012-12-19 05:18:35

标签: c# exception xna

我的xna游戏有这个“流氓”System.InvalidOperationException,它有时只会发生并且似乎是随机触发的,或者至少是我的控制输入明智的东西。按下开始并加载第一个菜单屏幕后,(我使用GameStateManagement示例作为我的游戏的基础,如果这有帮助)游戏有时但不总是在“in”中抛出System.InvalidOperationException “foreach(屏幕中的GameScreen屏幕)”的一部分“方法的完整代码:

            /// <summary>
    /// Tells each screen to draw itself.
    /// </summary>
    public override void Draw(GameTime gameTime)
    {

        foreach (GameScreen screen in screens) //exception thrown at the "in" part
        {
            if (screen.ScreenState == ScreenState.Hidden)
                continue;

            screen.Draw(gameTime);
        }
    }

对于触发此异常的原因,我真的很无能为力,因为我没有做什么不同于什么时候它抛出异常而什么时候没有。我不确定这是否会对你有所帮助,但这是例外情况

   at System.Collections.Generic.List`1.Enumerator.MoveNext()
   at GameStateManagement.ScreenManager.Draw(GameTime gameTime)
   at Microsoft.Xna.Framework.Game.Draw(GameTime gameTime)
   at GameStateManagement.GameStateManagementGame.Draw(GameTime gameTime)
   at Microsoft.Xna.Framework.Game.DrawFrame()
   at Microsoft.Xna.Framework.Game.Tick()
   at Microsoft.Xna.Framework.Game.HostIdle(Object sender, EventArgs e)
   at Microsoft.Xna.Framework.GameHost.OnIdle()
   at Microsoft.Xna.Framework.XboxGameHost.RunOneFrame()
   at Microsoft.Xna.Framework.XboxGameHost.Run()
   at Microsoft.Xna.Framework.Game.RunGame(Boolean useBlockingRun)
   at Microsoft.Xna.Framework.Game.Run()
   at GameStateManagement.Program.Main()
}   System.Exception {System.InvalidOperationException}

这是调用堆栈:

>   GameStateManagementSample.exe!GameStateManagement.ScreenManager.Draw(Microsoft.Xna.Framework.GameTime gameTime) Line 338 + 0x33 bytes   C#
Microsoft.Xna.Framework.Game.dll!Microsoft.Xna.Framework.Game.Draw(Microsoft.Xna.Framework.GameTime gameTime) + 0x4d bytes  
GameStateManagementSample.exe!GameStateManagement.GameStateManagementGame.Draw(Microsoft.Xna.Framework.GameTime gameTime) Line 89 + 0x7 bytes   C#
Microsoft.Xna.Framework.Game.dll!Microsoft.Xna.Framework.Game.DrawFrame() + 0x6a bytes  
Microsoft.Xna.Framework.Game.dll!Microsoft.Xna.Framework.Game.Tick() + 0x2a9 bytes  
Microsoft.Xna.Framework.Game.dll!Microsoft.Xna.Framework.Game.HostIdle(object sender, System.EventArgs e) + 0x6 bytes   
Microsoft.Xna.Framework.Game.dll!Microsoft.Xna.Framework.GameHost.OnIdle() + 0x19 bytes 
Microsoft.Xna.Framework.Game.dll!Microsoft.Xna.Framework.XboxGameHost.RunOneFrame() + 0xc bytes 
Microsoft.Xna.Framework.Game.dll!Microsoft.Xna.Framework.XboxGameHost.Run() + 0x1b bytes    
Microsoft.Xna.Framework.Game.dll!Microsoft.Xna.Framework.Game.RunGame(bool useBlockingRun) + 0x9c bytes 
Microsoft.Xna.Framework.Game.dll!Microsoft.Xna.Framework.Game.Run() + 0x7 bytes 
GameStateManagementSample.exe!GameStateManagement.Program.Main() Line 108 + 0x6 bytes   C#

谢谢! Ivatrix。

1 个答案:

答案 0 :(得分:1)

您的游戏代码似乎正在修改您未考虑的某些代码路径上的screens。请查看IEnumerator.MoveNext上的文档。

  

在创建枚举数后修改了该集合。