SpriteBatch: "You cannot start again until End is successfully called."

So, I started my current project, and the first thing I did was run it, and this gave me the above exception. Everything was fine last night. This is all the code in my Draw event. spriteBatch.Begin no longer appears in the project. Removing Begin here causes spriteBatch.Draw to throw an exception, placing spriteBatch.End just before the start, also throws an exception. I donโ€™t understand what happened and how to fix it.

GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); spriteBatch.Draw(background, Vector2.Zero, Color.White); player.Draw(spriteBatch); level1.Draw(spriteBatch); spriteBatch.End(); base.Draw(gameTime); 
+6
source share
4 answers

Well, I need to see your levek1.Draw and player.Draw for my information, but that I expect this. In one of your methods, you call spriteBatch.Begin() , you MUST call End() before you call it.

So, if your playerโ€™s class drawing method is as follows

 Draw(Spritebatch spriteBatch) { spriteBatch.Begin(); spriteBatch.Draw(PlayerSprite,PlayerPosition, etc); spriteBatch.End(); } 

You need to remove the Begin and End calls because they have already been called by the parent method.

EDIT: I didnโ€™t read it clearly, I see that you believe that this is not a problem, but we need to see other methods to find out the problem.

+1
source

It is possible that one of the calls to your draw raises an exception. This will kick you out of the method without calling spriteBatch.End() , and then the next time you will get an exception in spriteBatch.Begin() . (Although I would be wondering why the first exception did not finish your program, and the second did.)

If this is a problem, one solution would be to wrap the drawing calls in a try / finally block:

 spriteBatch.Begin(); spriteBatch.Draw(background, Vector2.Zero, Color.White); try { player.Draw(spriteBatch); level1.Draw(spriteBatch); } finally { spriteBatch.End(); } 

Another possibility is that you actually accidentally call spriteBatch.Begin() twice. Personally, I do not do this by wrapping a SpriteBatch object in another class.

Example:

 internal sealed class DrawParams { private SpriteBatch mSpriteBatch; private bool mBegin; /// <summary>Calls SpriteBatch.Begin if the begin value is true. Always call this in a draw method; use the return value to determine whether you should call EndDraw.</summary> /// <returns>A value indicating whether or not begin was called, and thus whether or not you should call end.</returns> public bool BeginDraw() { bool rBegin = mBegin; if (mBegin) { mSpriteBatch.Begin(); mBegin = false; } return rBegin; } /// <summary>Always calls SpriteBatch.End. Use the return value of BeginDraw to determine if you should call this method after drawing.</summary> public void EndDraw() { mSpriteBatch.End(); } } 
+1
source

Press ctrl+F and select Entire Solution , find spriteBatch.Begin() and delete it where it does not belong. I suspect you might have extra copies in your player or level calls.

Also make sure that you use the right variable for your sprite lot, it can be called SpriteBatch , with a capital letter.

Anyway, you can try to remove (comment them out with // ) all the spritebatch calls from your project and see if this throws an exception.

0
source

I had this exact problem and none of the suggestions worked here. Finally, I realized that in the "Start / End" section, I was await with an asynchronous method.

Realizing that this is possible after restarting the method to completion, I simply added the bool isDrawing variable and checked it before drawing.

NTN

0
source

All Articles