Rendering between layers

Mar 21, 2011 at 2:29 AM

Hello all!

I've been trying to get this working for a while... I'm trying to get my character to render after a specific layer has been rendered. Here's the logic I'm using (this is not my actual code):

foreach (Layer layer in map.Layers)
         layer.Draw(mapDisplayDevice, new Location(0, 0), viewport);
         if (layer.Id == "1")
              //Just after rendering walking layer, now draw characters


I've tried reversing my layer ordering (just to be sure) but no matter what my character renders over the map. I'm using SpriteBatch to render the character, should I be using tIDE somehow? Any help is greatly appreciated! :)

Mar 21, 2011 at 9:44 AM


You don't need to replicate the Map rendering code to render graphics between layers. You just need to hook an event handler to the BeforeDraw or AfterDraw events of the relevant Layer object and just call the Map's Draw method as in the tutorial.

I should probably write a How-To help page some time or other.



Mar 26, 2011 at 7:26 PM

Thanks, all working now :)

Aug 13, 2011 at 5:27 PM

Any chance of getting an example for this? I'm not really sure how to go about adding an event handler without getting into the source...

Aug 13, 2011 at 6:53 PM
Edited Aug 13, 2011 at 6:56 PM

The following code is from the demo, which uses the Layer's BeforeDraw event to render falling leaves between layer 2 and layer 3 (second to topmost and topmost).


To hook an event to a layer, you would do something like this in your initialisation code:

m_map.Layers[3].BeforeDraw += OnBeforeLayerDraw;


The event handler would then look something like this: 

private void OnBeforeLayerDraw(object sender, LayerEventArgs layerEventArgs)
    SpriteBatch spriteBatch = m_xnaDisplayDevice.SpriteBatchAlpha;

    // do stuff here...


Aug 13, 2011 at 8:41 PM

Awesome! Now, is there any way to use this with the wrap function, or do I need to draw my characters again at the right offsets?

Aug 13, 2011 at 9:53 PM

You'll have to do the character wraparound yourself - the engine just gives you the opportunity to render stuff between layers. 

Feb 17, 2013 at 6:21 PM
This might sound like a dumb question, but i'm having similar problems, I cant get it to render between the layers. I got the hook event setup to render after the first layer, layer[0]. But when i attempt to draw. it either comes up behind all the layers or in front of all the layers. What im wondering is, where do you get this m_xnaDisplayDevice.SpriteBatchAlpha?

I have my IDisplayDevice idd
idd = new XnaDisplayDevice(this.Content, this.GraphicsDevice);

but it doesn't contain .SpriteBatchAlpha, So it seems i'm missing a step or something?
Feb 18, 2013 at 9:46 AM
Edited Feb 18, 2013 at 9:47 AM
The SpriteBatchAlpha and SpriteBatchAdditive properties are specific to XnaDisplayDevice and hence do not show up as properties in IDisplayDevice .

Try changing you idd type to XnaDisplayDevice .