I have the main game (Game1.cs) draw the sprites like so:
//Clear everything
GraphicsDevice.Clear(Color.CornflowerBlue);
//Begin drawing
_spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
//Draw enemies
for (int i = 0; i < ControlObj.EnemyList.Count; i++)
{
ControlObj.EnemyList[i].DrawObject(_spriteBatch);
}
//Draw powerups
for (int i = 0; i < ControlObj.PowerupList.Count; i++)
{
ControlObj.PowerupList[i].DrawObject(_spriteBatch);
}
//Draw other stuff
for (int i = 0; i < ControlObj.OtherStuffToDraw.Count; i++)
{
ControlObj.OtherStuffToDraw[i].DrawObject(_spriteBatch);
}
//Draw the player
ControlObj.PlayerObj.DrawObject(_spriteBatch);
//Finish drawing
_spriteBatch.End();
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)
{
base.DrawObject(sb);
for (int i = 0; i < OtherStuffList.Count; i++)
{
OtherStuffList[i].DrawObject(sb);
}
}
For some reason, objects drawn by other objects do not obey their depth property. How could I fix this?