This project is read-only.

TMX Tile Map Rendering Order

Topics: Sprites and SpriteBatch
Oct 4, 2013 at 12:24 PM
Is there an established way to control the render order of the individual tiles of a tile map?

When using other libraries, I've historically either pushed each tile into a render queue such that my desired render order was achieved or iterated through the relevant tiles and assigned an appropriate depth / z-index-value.

Here, I've tried to do the latter; however, it didn't deliver the desired results. My attempt basically involved overloading the (Cocos) layer's draw method, iterating through each relevant (TMX) layers' tiles by using the TileAt method and assigning an appropriate zOrder to the returned sprite, and then calling the base.Draw() to render everything. The TMX tile map had been added to the (CC) layer as a child. Like I said, it was (or seemed to be) a no-go.

My desired result is to render the images such that an overhead-45-degree-perspective is believable. That is to say, if the base of an image is rendered slightly above the base of a tile image in an appropriate (TMX) layer, then the tile would be rendered before the image and appear to be "in front of" the image. So, I would have certain layers that would always be rendered first (e.g. ground), certain layers that would always be rendered last (e.g. tree tops), and the other layers would be rendered relative to the non-tile images (e.g. sign posts).

Thanks for taking the time!
Oct 7, 2013 at 11:55 PM
Reading the cocos2d-x documentation, I found that there is built-in relative ordering.

So, I've given my Ground layer a "cc_vertexz" property of -1000, my GroundObjects layer a cc_vertexz property of automatic, I've added the test character sprite to the tile map as a child, and I've got it setup to calculate and update it's VertexZ value before drawing.

I threw
CCLog.Log( pos.ToString() + " = " + ret.ToString() );
into CCTMXLayer in VertexZForPos() to make sure that it was being used and to easily see which tiles had what z value and saw the output that I expected. Unfortunately, though, it was not being rendered by VertexZ -- it was still rendering by child order (interestingly, it was putting my sprite between the two tile map layers; above the ground, under the ground objects).

I did some playing around with ReorderChild, using it to force the sprite to be reordered in the child list. Reordering the sprite with 1 would draw it after everything, with 0 would draw it before the ground and after the ground objects, and with -1 would draw it first.

Is there any setup that I'm missing? Is this fully implemented in cocos2d-xna?

Any insight would be a big help!