Pilispring
Pilispring

Reputation: 97

Why does a drawing appear before the others in spritebatch?

i have a problem in my program. I don't understand how a screen is taking over everything else in the screen. In details, my code looks like this:

public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
    {
        spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, null, null, cam.get_transformation(GestionEcran.GraphicsDevice));

        //Draw the background
        decor.Draw(spriteBatch, gameTime, 255);

        //Draw the players
        Player2.Draw(spriteBatch, gameTime);
        Player1.Draw(spriteBatch, gameTime);

        //Draw the foreground
        decor.DrawPremierPlan(spriteBatch, gameTime, 70);


        if (aPlayerisWinning== true)
        {
            //THIS LINE "HITBOX" IS APPEARING BEFORE PLAYERPORTRAIT!!!!!!!!!!!!!!
            spriteBatch.Draw(hitBox, new Rectangle(0, 0, 500,500), new Color(255, 255, 255, 255));
            spriteBatch.Draw(PlayerPortrait, new Rectangle(0,0,500,500), new Color(255, 255, 255, 255));
        }
        spriteBatch.End();

        // USED FOR ADDITIVE BLENDING

        spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive, null, null, null, null, cam.get_transformation(GestionEcran.GraphicsDevice));
        {
            Player1.DrawEffects(spriteBatch,gameTime);
            Player2.DrawEffects(spriteBatch,gameTime);
        }
        spriteBatch.End();

As you can see, hitbox (a white screen) have to appear behind PlayerPortrait but it doesn't work. The white Screen covers every other drawings.

Do you have any idea? Thx.

EDIT: nothing is changing with SpriteSortMode. BacktoFront, FrontToBack and Texture mess with my overlapping structure. Immediate and Deffered works well but have the same problem with the white screen. As said in the link of the first answer, deffered draw all the sprites in one batch, in the same order calls to Draw were received. So why my second to last call appears before my last call?

Upvotes: 0

Views: 100

Answers (2)

Pilispring
Pilispring

Reputation: 97

I found a solution by playing with the alpha of each draw. It does the effect i wanted and bypassing the problem

Solution 2: When playing with the alpha channel to play on the transparency, you need to manipulate the RGB too for the screen to be transparent. I didn't know.

Upvotes: 1

Cyral
Cyral

Reputation: 14153

Check out the other SpriteSortMode enumerators.

You can change the draw order by using BackToFront and FrontToBack, which are really just variations of Deferred mode. Texture is another version of Deferred and it will sort textures thus also gaining performance.

Upvotes: 2

Related Questions