views:

162

answers:

1

I would like some 2D sprites to occlude some 3D DrawableGameComponents. For a simplified example, a pause menu (implemented as a 2D sprite) should be drawn on top of a paused space ship (implemented as 3D game component).

The XNA framework automatically calls Draw methods of classes from Microsoft.Xna.Framework.Game and Microsoft.Xna.Framework.DrawableGameComponent.

In my experience, I am finding that the draw order is:

  1. Game Draw() methods
  2. DrawableGameComponent Draw() methods

My spriteBatch draw method calls are the draw method of my Game class. Therefore, the space ship is drawn over the top of the pause menu (making it unreadable).

It seems to me that the simplest way to solve my problem would be to reverse the order that the draw methods are called. Is this possible?

I have tried setting the DrawOrder of the DrawableGameComponents to different values (1000, 1, 0, -1, -1000) but it has no effect on the XNA framework Draw() method call order between instances of Game and DrawableGameComponent.

I have looked into the layerDepth of SpriteBatch Draw method calls, but they appear to be unrelated to the XNA Framework draw order between the Game and DrawableGameComponent classes. Layer depth only seems to affect the sprites in the SpriteBatch.

I am new to using XNA but I suspect that I am asking too much of the framework. If this is the case, what would be the normal way to achieve the placing of 2D objects on top of 3D objects?

Any assistance would be appreciated.

+1  A: 

There has been a while since I used XNA, but I believe the base.Draw() call in the Draw() method of your Game class will iterate through your DrawableGameComponents, so you could try to place this call before or after the drawing logic in your Draw method depending on what order you want the calls to happen.

Heap
Exactly right! I did not know that base.Draw() was calling the DrawableGameComponent Draw() method. Moving the base.Draw() method call above the drawing logic fixed the draw order. Thank you.
Jeremy Larter