Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.