I've got a tricky issue I'm not sure how to tackle best: I have an animated tile of a door. When it's closed it should be sorted one way, but when it's openend it will need to be sorted a different way, as it belonging to a different(neighbouring tile).

Here's the door closed:

door closed

and the door opened:

door opened

I imagine it would be possible to override the sorting system for such tiles and adjust the sorting based on the frame, but it feels a bit hacky.

Has anyone encountered a similar scenario ? Any elegant solutions ?

  • 2
    \$\begingroup\$ How does your sorting system currently detect the draw order of tiles? \$\endgroup\$
    – Roy T.
    Commented Jun 21, 2012 at 17:45

4 Answers 4


Maybe you could make a door out of two objects, on different tiles, and only show one of them at a time. This way, opened door might conceivably block movement too, which seems kinda realistic.


There are 2 tricks I use for sorting in 2D:

  • Add ground position to sprites, this is a point where sprite touches the ground (f.e. mens feet, trees trunk, doors frame) and sort by it instead of sprites pixels. This way sprite will not be affected by animation

  • Add non-sortable childs to objects (f.e. men can have several childs which do not affect sorting - cloak, bow, sword, etc, door frame has child door). This way animation of child sprites does not affects sorting either.


Create three sprites...

  1. Door closed (like your first sprite) - blocks movement
  2. Just the frame (post in your case) of the door, but no door - doesn't block movement
  3. Just the door in open position - blocks movement

When the door is closed, draw sprite 1, and treat it as a "blocker" so players can't walk through it.

When the door is opened, replace that sprite with #2 (just the door post). Then place sprite #3 (just the door) in the adjacent cell that the door would be in.

Closing is just removing the door, and replacing the post with post+door in closed position.

With this setup, you don't have to worry about rendering the opened door in any special order. Just render it in the order as any other object in the square it is in.

The downside is that you can't have the player or any other object in the square where the opened door will go.


The best way to solve the sorting of objects is by not sorting at all. If you use a depth buffer, you can render everything in one pass. Objects that have a higher z-value will be further away than objects with a lower z-value.

Even if your game is 2D, you should still consider using 3D coordinates with a depth buffer, otherwise you will forever tango with the painter's algorithm.

  • \$\begingroup\$ True, but how to determine Z position? Only by sorting \$\endgroup\$
    – Kromster
    Commented Jun 22, 2012 at 6:46
  • \$\begingroup\$ Yes, but that sorting now happens offline, instead of during rendering. Now the level designer can build the level with the Z values in mind, without having to worry about clipping objects. \$\endgroup\$
    – knight666
    Commented Jun 22, 2012 at 6:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .