1
\$\begingroup\$

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.DrawString(font, "àéùaDASASCZadhkjashdash", new Vector2(4, 108), Color.White);
            spriteBatch.End();
        }
    }

This happens:

enter image description here

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.

enter image description here

Or this (if I only comment the character line)

enter image description here

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

enter image description here

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);
spriteBatch.DrawString(font, "àéùaDASASCZadhkjashdash", new Vector2(4, 108), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0f);
\$\endgroup\$
3
  • \$\begingroup\$ 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. \$\endgroup\$
    – user74224
    Jun 27, 2016 at 20:57
  • \$\begingroup\$ cool looking game btw. \$\endgroup\$
    – jgallant
    Jun 27, 2016 at 22:52
  • \$\begingroup\$ 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. \$\endgroup\$
    – user74224
    Jun 28, 2016 at 9:23

1 Answer 1

1
\$\begingroup\$

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);
\$\endgroup\$
7
  • \$\begingroup\$ 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. \$\endgroup\$
    – user74224
    Jun 27, 2016 at 19:32
  • \$\begingroup\$ Do this for all your draw calls,making sure you set that float value for the layerdepth accordingly \$\endgroup\$
    – jgallant
    Jun 27, 2016 at 19:35
  • \$\begingroup\$ 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". \$\endgroup\$
    – user74224
    Jun 27, 2016 at 20:01
  • 2
    \$\begingroup\$ the layerdepth value must be between 0.0f, and 1.0f \$\endgroup\$
    – jgallant
    Jun 27, 2016 at 20:03
  • 3
    \$\begingroup\$ 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." \$\endgroup\$
    – jgallant
    Jun 27, 2016 at 20:10

You must log in to answer this question.