Search code examples
c#xna

XNA ArgumentOutOfRangeException was unhandled


relatively new to programming here, and this is my first "large" independent project; it's a platformer game. So when I look at the code and debug it I understand what it's saying and what the error is, the problem is I can't figure out what the solution is...I've tried tons of different things. Here's where I think the error is. In my splash screen class;

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

and then this is the AddScreen method from my screen manager class;

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

the problem may extend to the fade animation class too? but I figured I would start with this. When the error occurs, animation count is 1, and imageNumber is also 1.


Solution

  • You increment imageNumber and have a guard for when it exceeds the max, but nowhere do you reset it back to 0 again.

    Change:

    if (imageNumber >= animation.Count - 1 || inputManager.KeyPressed(Keys.Enter))
    {                    
        ScreenManager.Instance.AddScreen(new TitleScreen(), inputManager);
    }   
    

    ...to:

    if (imageNumber >= animation.Count - 1 || inputManager.KeyPressed(Keys.Enter))
    {                    
        ScreenManager.Instance.AddScreen(new TitleScreen(), inputManager);
        imageNumber = 0;  // reset the sequence
    }   
    

    ...in your Update() method.