# Why is Spritebatch drawing my Textures out of order?

I just started working with XNA Studio after programming 2D games in java. Because of this, I have absolutely no experience with Spritebatch and sprite sorting. In java, I could just layer the images by calling the draw methods in order. For a while, my Spritebatch was working fine in deferred sorting mode, but when I made a change to one of my textures, it suddenly started drawing them out of order. I have searched for a solution to this problem, but nothing seems to work. I have tried adding layer depths to the sprites and changing the sort mode to BackToFront or FrontToBack or even immediate, but nothing seems to work. Here is my drawing code:

    protected override void Draw(GameTime gameTime) {
GraphicsDevice.Clear(Color.Gray);
Game1.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null);

for (int x = 0; x < 5; x++) {
for (int y = 0; y < 5; y++) {
region[x, y].draw(((float)w / aw)); // Draws the Tile-Based background
}
}

player.draw(spriteBatch, ((float)w / aw));//draws the character (This method is where the problem occurs)
enemy.draw(spriteBatch, (float)w/aw); // draws a basic enemy
Game1.spriteBatch.End();
base.Draw(gameTime);
}


Player.Draw():

public void draw(SpriteBatch sb, float ratio){
//draws the player base (The character without hair or equipment)
sb.Draw(playerbase[0], new Rectangle((int)(pos.X - (24 * ratio)), (int)(pos.Y - (48 * ratio)), (int)(48 * ratio), (int)(48 * ratio)), new Rectangle(orientation * 48, animFrame * 48, 48, 48), Color.White,0,Vector2.Zero,SpriteEffects.None,0);
//draws the player's hair
sb.Draw(playerbase[3], new Rectangle((int)(pos.X - (24 * ratio)), (int)(pos.Y - (48 * ratio)), (int)(48 * ratio), (int)(48 * ratio)), new Rectangle(orientation * 48, animFrame * 48, 48, 48), Color.White, 0, Vector2.Zero, SpriteEffects.None, 0);
//draws the player's shirt
sb.Draw(equipment[0], new Rectangle((int)(pos.X - (24 * ratio)), (int)(pos.Y - (48 * ratio)), (int)(48 * ratio), (int)(48 * ratio)), new Rectangle(orientation * 48, animFrame * 48, 48, 48), Color.White, 0, Vector2.Zero, SpriteEffects.None, 0);
//draws the player's pants
sb.Draw(equipment[1], new Rectangle((int)(pos.X - (24 * ratio)), (int)(pos.Y - (48 * ratio)), (int)(48 * ratio), (int)(48 * ratio)), new Rectangle(orientation * 48, animFrame * 48, 48, 48), Color.White, 0, Vector2.Zero, SpriteEffects.None, 0);
//draws the player's shoes
sb.Draw(equipment[2], new Rectangle((int)(pos.X - (24 * ratio)), (int)(pos.Y - (48 * ratio)), (int)(48 * ratio), (int)(48 * ratio)), new Rectangle(orientation * 48, animFrame * 48, 48, 48), Color.White, 0, Vector2.Zero, SpriteEffects.None, 0);
}


the game has a top-down perspective much like the early legend of zelda games. It draws sections of the texture depending on which direction the character is facing and the animation frame. However, instead of drawing the character in the order the draw methods are called, it ends up drawing the character out of order. Please help me with this problem.

• Have you tried SpriteSortMode.Immediate? – Martin Nov 25 '12 at 5:35
• Are Game1.spriteBatch and spriteBatch really pointing to the same SpriteBatch object? – David Gouveia Nov 25 '12 at 18:06
• I have tried SpiteSortMode.Immediate and it seems to have no effect. Game1.spriteBatch points to a public static spriteBatch object that is used in both draw methods. I have also tried passing the spriteBatch into the player.draw() method with the same result. Is this a glitch with XNA or is my code messed up? If you have any suggestions as to how to arrange my code or how to make better draw methods, feel free to comment. – Andrew Nov 25 '12 at 18:52
• Sprites are always drawn in the order that they're called when using deferred or immediate mode. But from your code I can't tell if you're really passing the same spritebatch to player.draw() and enemy.draw() than the one you called Begin() on. For consistency, shouldn't it be player.draw(Game1.spriteBatch, ...) ? – David Gouveia Nov 25 '12 at 21:47
• I tried passing the SpriteBatch through the draw method, but not with the example of code that I showed. This segment of code is the code I was using with the static variable. It was working correctly until I made a change to one of the textures, but now, no matter what I do, it always draws them out of order. Should I post an example with the spriteBatch being passed into the method? – Andrew Nov 25 '12 at 22:14

The way I always think of it is like photoshop layering. If layer1 is placed before layer2. When you draw them, layer2 will be in front of layer1. If you want picture1 to be on top of picture2, you have to make sure you draw it after picture2. I suggest you find a friend or someone that can draw digitally cause the way you're doing, it's going to take you decades to finish the game.

A more concrete example:

//Base sb.Draw(playerbase[0], new Rectangle((int)(pos.X - (24 * ratio)), (int)(pos.Y - (48 * ratio)), (int)(48 * ratio), (int)(48 * ratio)), new Rectangle(orientation * 48, animFrame * 48, 48, 48), Color.White,0,Vector2.Zero,SpriteEffects.None,0);

//draws the player's hair sb.Draw(playerbase[3], new Rectangle((int)(pos.X - (24 * ratio)), (int)(pos.Y - (48 * ratio)), (int)(48 * ratio), (int)(48 * ratio)), new Rectangle(orientation * 48, animFrame * 48, 48, 48), Color.White, 0, Vector2.Zero, SpriteEffects.None, 0);

In this case, base is in the back and it's hair is in front. According to what you have the lowest layer is the player's base and the highest layer is it's shoes. Seems ok to me, can you share a print screen of how the player is being drawn?

This might be because you have them listed out of order in the spritebatch.Draw() section. XNA draws images onto the screen from top to bottom, meaning that the images you have further down the drawing list, the higher up they'll appear in the game.

The Player.draw() method does the same thing.