LukeP
LukeP

Reputation: 10422

Why is this code drawing sprites in reverse order?

I've got the following piece of code:

spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend, null, null, null, null, cam.TransformMatrix);
spriteBatch.Draw(_comicBackground, workspace, Color.AliceBlue);
foreach (LayoutField lf in comicStrip.LayoutFields)
{
    spriteBatch.Draw(_layoutFieldBackground, new Rectangle(lf.PosX, lf.PosY, lf.Width, lf.Height), Color.White);
}
spriteBatch.End();

but regardless of SpriteSortMode (FrontToBack | BackToFront) background is always being displayed on top of the results of foreach loop, covering my layout in the result.

It's not a show-stopper as I can draw my sprites in reverse order but I'd rather keep them in the order in which they should be drawn on the screen.

Upvotes: 0

Views: 300

Answers (1)

Cole Campbell
Cole Campbell

Reputation: 4857

The FrontToBack and BackToFront sort modes require you to specify a sprite depth value in your Draw() calls; see the layerDepth parameter here. If you want to draw sprites in the order that you make the Draw() calls, use SpriteSortMode.Deferred or SpriteSortMode.Immediate.

Upvotes: 4

Related Questions