C# XNA ArgumentOutOfRange异常未处理
在这里编程相对较新,这是我的第一个“大型”独立项目;这是一场平板游戏。因此,当我查看代码并调试它时,我理解它在说什么,错误是什么,问题是我无法找出解决方案是什么……我尝试了很多不同的方法。这就是我认为错误的地方。在我的闪屏课上C# XNA ArgumentOutOfRange异常未处理,c#,xna,C#,Xna,在这里编程相对较新,这是我的第一个“大型”独立项目;这是一场平板游戏。因此,当我查看代码并调试它时,我理解它在说什么,错误是什么,问题是我无法找出解决方案是什么……我尝试了很多不同的方法。这就是我认为错误的地方。在我的闪屏课上 public override void Update(GameTime gameTime) { inputManager.Update(); Animation a = animation[imageNumber]; FA
public override void Update(GameTime gameTime)
{
inputManager.Update();
Animation a = animation[imageNumber];
FAnimation.Update(gameTime, ref a);
animation[imageNumber] = a;
if (animation[imageNumber].Alpha == 0.0f)
imageNumber++;
if (imageNumber >= animation.Count - 1 || inputManager.KeyPressed(Keys.Enter))
{
ScreenManager.Instance.AddScreen(new TitleScreen(), inputManager);
}
}
public override void Draw(SpriteBatch spriteBatch)
{
animation[imageNumber].Draw(spriteBatch); ------> (ERROR)
}
这是我的screen manager类中的AddScreen方法
public void AddScreen(GameScreen screen, InputManager inputManager)
{
transition = true;
newScreen = screen;
fade.IsActive = true;
fade.Alpha = 0.0f;
fade.Increase = true;
fade.activateValue = 1.0f;
this.inputManager = inputManager;
}
问题可能也会延伸到淡入淡出动画类?但我想我应该从这个开始。发生错误时,动画计数为1,imageNumber也为1 您增加了
imageNumber
,并在超出最大值时设置了一个防护,但您无法再次将其重置为0
更改:
if (imageNumber >= animation.Count - 1 || inputManager.KeyPressed(Keys.Enter))
{
ScreenManager.Instance.AddScreen(new TitleScreen(), inputManager);
}
…致:
if (imageNumber >= animation.Count - 1 || inputManager.KeyPressed(Keys.Enter))
{
ScreenManager.Instance.AddScreen(new TitleScreen(), inputManager);
imageNumber = 0; // reset the sequence
}
…在您的
Update()
方法中。数组索引从0开始,因此如果您的计数为1,则您的图像编号正在尝试访问超出范围的索引。谢谢!这带来了一系列新的问题,但给我带来麻烦的不是那个错误@好的。如果你愿意,你可以随时把它作为一个新问题发布。如果你认为是这样的话,就接受这个答案。祝你周末愉快