# Are there spritebatch drawing limitations on monogame?

I've reached a point that it seems when I draw too much on screen, monogame starts doing weird things.

When I call this code in the Draw method

public void drawDialogBox()
{
if (dialogueBoxOn == 1)
{
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.NonPremultiplied, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullNone, null, Matrix.CreateScale(GameGlobals.zoom));
spriteBatch.Draw(dialogBoxChara, new Vector2(89, 18), Color.White);
spriteBatch.Draw(dialogBoxTexture, new Vector2(0, 108), Color.White);
spriteBatch.End();
}
}


This happens:

This code is supposed to show the character, the textBox itself and the text. But the text isn't drawn correctly. If I comment one of the draw lines, it works.

Or this (if I only comment the character line)

Any ideas of what is happening here? I thought it was some SpriteBatch limitations, but I don't know...

EDIT: After following @Jon advice, i've reached this state

Now it shows the character and dialog properly, but the background and the walls are not displayed now. I have all the walls like this:

spriteBatch.Draw(leftcenterright4, new Vector2(0, 53), null, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0.5f);


And the background

spriteBatch.Draw(background, new Vector2(0, 0), null, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 1f);


And the code from earlier fixed:

spriteBatch.Draw(dialogBoxChara, new Vector2(89, 18),null, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0f);
spriteBatch.Draw(dialogBoxTexture, new Vector2(0, 108),null, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0.1f);

• Somehow I've managed to solve this. With the help of @Jon. What I did, first, set all the layerdepth acordingly, after that, I've tried to change the resolution multiplier of my game to 1, and...It was working. Now it seems I've been causing the problem by trying to make zoom directly and not with RenderTarget2D. I will try to learn how to use that. – user74224 Jun 27 '16 at 20:57
• cool looking game btw. – jgallant Jun 27 '16 at 22:52
• Thanks @Jon. Also, I've finally get it to work as intended. With Jon's help here and on monogame forums. community.monogame.net/t/scaling-zooming-game-window/7824/16 I'll leave the link here, maybe will need it sometime. – user74224 Jun 28 '16 at 9:23

Use an override of the SpriteBatch.Draw() function in order to specify what layer you want to draw your sprites on. You are already specifying "FrontToBack" sort mode in your initialization so that is good.

Take a look here: https://msdn.microsoft.com/en-us/library/ff433988.aspx

More specifically, take a look at the "layerDepth" property. This is what you are looking for. Set this according to the order you want to draw your sprites on.

spriteBatch.Draw (texture, position, null, color, 0, Vector2.Zero, 1, SpriteEffects.None. 0.5f);

• spriteBatch.Draw(dialogBoxChara, new Vector2(89, 18),null, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0.5f); I've tried this on the character, but the sprite isn't showing now. – user74224 Jun 27 '16 at 19:32
• Do this for all your draw calls,making sure you set that float value for the layerdepth accordingly – jgallant Jun 27 '16 at 19:35
• I can't get it to work. I've also tried in a new window with only one sprite, and to no avail. spriteBatch.Draw(mainMenuIndexSpr, new Vector2(89, 18), null, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, -100000f); I've tried diferent values for "layerdepth". – user74224 Jun 27 '16 at 20:01
• the layerdepth value must be between 0.0f, and 1.0f – jgallant Jun 27 '16 at 20:03
• Directly from the link I posted... "The depth of a layer. By default, 0 represents the front layer and 1 represents a back layer. Use SpriteSortMode if you want sprites to be sorted during drawing." – jgallant Jun 27 '16 at 20:10