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 have the main game (Game1.cs) draw the sprites like so:

        //Clear everything

        //Begin drawing
        _spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);

        //Draw enemies
        for (int i = 0; i < ControlObj.EnemyList.Count; i++)

        //Draw powerups
        for (int i = 0; i < ControlObj.PowerupList.Count; i++)

        //Draw other stuff
        for (int i = 0; i < ControlObj.OtherStuffToDraw.Count; i++)

        //Draw the player

        //Finish drawing

DrawObjects method:

        public virtual void DrawObject(SpriteBatch sb)
        int imgIndex = (int)Math.Floor(ImageIndex);

        //Draw the object!
        sb.Draw(Sprite[imgIndex],   //Sprite (Texture2D)
                Center,             //Location of object (Vector2)
                null,               //Source rectangle (USELESS)
                ColorBlend,         //Color blend (Color)
                Rotation,           //Rotation angle (in radians)
                CenterOffset,       //Offset (Vector2)
                ImageScale,         //Image scale (Vector2)
                SpriteEffects.None, //Flip sprite? (USELESS)
                Depth);             //Depth of the object

The issue: Some objects draw other objects by overriding the DrawObject method like so:

        public override void DrawObject(SpriteBatch sb)

        for (int i = 0; i < OtherStuffList.Count; i++)

For some reason, objects drawn by other objects do not obey their depth property. How could I fix this?

share|improve this question
Is it only the "OtherStuffList" objects that are having an issue? Everything else renders at the depth expected? –  Djentleman Jan 21 '13 at 11:24
Everything that is not drawn by another object renders on the correct layer. –  Andrew Sun Jan 21 '13 at 11:44
This looks correct to me, How are you instantiating the new OtherStuffList objects depth? –  AbstractChaos Jan 21 '13 at 13:14
I use an enum and a dictionary. Upon starting the game I loop through the enum and store depth values (e.g. 0, 0.1, 0.2, etc) in the dictionary. Then when objects are created I just set their depth values using the values in the dictionary. –  Andrew Sun Jan 22 '13 at 1:14

3 Answers 3

To develop on what our fellow Xavier said, do not use that SpriteSortMode in your case. As it is right now, it doesn't matter the order objects are put to be drawn, they will be sorted by their layer value later, before being put on screen.

Use SpriteSortMode.Deferred (_spriteBatch.Begin(SpriteSortMode.Deferred /*!!*/, BlendState.AlphaBlend);) and this will most probably work. This one disables ordering, making it so that textures are drawn in the order they are called.

You can check out some more info on SpriteSortMode here, if you'd like.

share|improve this answer
I believe that Andrew is intentionally using BackToFront. As he implies in his question, objects have a depth property assigned so they aren't meant to be drawing in the order they appear in the Draw call. They're meant to be drawing based on the Depth property. –  Djentleman Jan 21 '13 at 22:40
Yeah, if there's really no other way, I would probably just let Game1 draw everything. What I really want to know is why having objects draw other objects is messing up the depth order. –  Andrew Sun Jan 22 '13 at 1:10

To implement the concept of layers you could call Begin, draw the layer's content and call End on the spritebatch (note that more Begin/End calls means loosing performance!).

Make sure that there aren't any sprites trying to be drawn on the same depth value, as the draw order is not defined for 2 sprites with the same depth value (causing random flickering).

Since you're using some kind of object hierarchy to draw your game I'd recommend you to avoid using depth values and use the hierarchic model through all the game (cocos2d handles it this way).

This way you can have a screen object that draws all the screen, and the order of it's children will decide who draws first. This idea, used recursively, decides all the relative depths for your sprites.

share|improve this answer
up vote 0 down vote accepted

Oops, I found the source of the issue... Turns out that having objects draw other objects works fine. The issue was that I initialized values to zero in objects' constructors like so:

    public GameObject(Game1 game) : base(game)
        game.Components.Add(this); //ISSUE HERE
        Depth = 0;

And I set their values per-object in Initialize():

        //Set the drawing depth
        Depth = _myGame.UtilitiesObj.GetDepth(Utilities.DrawingDepths.Missile);

The issue was that calling Game1.Components.Add() also called Initialize() immediately. So the values would be set to their correct values, and then "initialized" back to 0. I thought that Initialize() was always called AFTER the constructor, hence the bug.

share|improve this answer

Your Answer


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

Not the answer you're looking for? Browse other questions tagged or ask your own question.