Zackogenic
Zackogenic

Reputation: 11

XNA: How to make background stay in background?

I thought that the draw method would draw stuff Front to back, so that the first things to be drawn in the code would be in the front, and the last would be in the front. So, I'm trying to draw a tile background from a array, but some of it is in front of everything else, and some other parts are behind everything else. How do I fix this?

My draw code:

   protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(BackColor );

        spriteBatch.Begin(SpriteSortMode.FrontToBack,BlendState.AlphaBlend,null,null,null,null,m_Camera.Transform(graphics.GraphicsDevice ));

        Draw1();
        base.Draw(gameTime);
    }

This is the draw method

    public void Draw1()
    {

        DrawText();
        topwall.Draw(spriteBatch);
        bottomwall.Draw(spriteBatch);
        for (int i = 0; i < wallsize; ++i)
        {

            //new GameObject(Tech[i].texture, Tech[i].Position );

            Tech[i].Draw(spriteBatch);
        }
        Door1.Draw(spriteBatch);
        Door2.Draw(spriteBatch);
        //playerOne.Draw(spriteBatch);
        //playerTwo.Draw(spriteBatch);
        ball.Draw(spriteBatch);
        drawdirt();
        spriteBatch.End();
    }

This draws the background

    public void drawdirt()
    {
        for (int i = 0; i < world.GetUpperBound(0); ++i)
        {
            for (int n = 0; n < world.GetUpperBound(1); ++n) { world[i, n].Draw(spriteBatch); }
        }
    }

Upvotes: 1

Views: 3791

Answers (2)

Asik
Asik

Reputation: 22133

No, by default SpriteBatch draws in the order of your draw calls (intuitively enough). Whatever you draw first gets drawn first, and whatever you draw last gets drawn last, on top of everything else. So you want to draw your background elements first, then the entities, then the UI, etc.

Alternatively, you can specify a layer depth on each draw call and then use a custom sorting mode by specifying SpriteSortMode.FrontToBack on the call to SpriteBatch.Begin(). The layer depth is a real from 0 to 1, with 0 being the front and 1 being the back.

Upvotes: 3

dtsg
dtsg

Reputation: 4468

You can control the order in which objects are drawn by using the SpriteSortMode property before SpriteBatch.Begin() is called.

Further reading: http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.spritesortmode.aspx

Upvotes: 0

Related Questions