我的 xna 游戏有这个“流氓” System.InvalidOperationException,它有时只会发生并且似乎是随机触发的,或者至少是我无法控制的输入。按下开始并加载第一个菜单屏幕后,(如果有帮助,我使用 GameStateManagement 示例作为我的游戏的基础)游戏有时但并不总是在“foreach”的“in”部分抛出 System.InvalidOperationException(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#
谢谢!伊瓦特里克斯。