1

In my gamestate management system I've got a screen manager and a menuscreen, both added to my current game. The screen manager opens a spritebatch then calls the draw methods of all of the subscribed screens, but when I get to the first SpriteBatch.Draw call in my first subscribed screen I get an error saying that a SpriteBatch.Begin must be called previously, which it was.

ScreenManager Draw Code:

    public override void Draw(GameTime gameTime)
    {
        SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);

        for (int i = 0; i < ScreenList.Count; i++)
        {
            if (ScreenList[i].Visible)
                ScreenList[i].Draw(gameTime);
        }

        SpriteBatch.End();

        base.Draw(gameTime);
    }

and my MenuScreen Draw Code:

    public override void Draw(GameTime gameTime)
    {
        SpriteBatch.Draw(Background, new Rectangle(0, 0, ScreenManager.Game.Window.ClientBounds.Width, ScreenManager.Game.Window.ClientBounds.Height),
                                                   null, Color.White, 0f, new Vector2(Background.Width / 2, Background.Height / 2), SpriteEffects.None, 1f);

        SpriteBatch.DrawString(Font, Text, Position, Color, 0f, Origin, Scale, SpriteEffects.None, 0f);

        foreach (MenuEntry entry in EntryList)
        {
            entry.Draw(gameTime);
        }
    }

and finally my base game initialize code where I create all these components:

    protected override void Initialize()
    {
        screenManager = new ScreenManager(this, Content.Load<SpriteFont>("Fonts/DefaultFont"), Content.Load<Texture2D>("Images/Background"));
        Components.Add(screenManager);

        screenManager.AddScreen(new MainMenuScreen(screenManager, "Tanks", new Vector2(screenManager.Game.Window.ClientBounds.Width / 2, 100)));

        base.Initialize();
    }

NOTE: MainMenuScreen inherits from MenuScreen which is an abstract class. Also my MenuScreen class gets passed a spritebatch from the ScreenManager class so they should be the same spriteBatch.

Nick
  • 177
  • 1
  • 8
  • Well I've figured out it would just be better to pass along a spritebatch in subsequent Draw() calls instead of in the classes themselves to make sure its the same spritebatch, if its possibly my previous way that would be cool but I've solved my own problem as of right now. – Nick Mar 01 '13 at 16:37
  • I had problems with that once and settled on the same solution and am content with that for now. If you solve the problem in another way post an answer and let us all know. – SpartanDonut Mar 01 '13 at 17:33
  • 1
    You should post your answer as an answer, not a comment. – user1306322 Mar 02 '13 at 19:08

0 Answers0