Home | Documentation | Atomic Chat | Github

Draw order for 2D Sprites



I’m struggling with the right way to get sprites to draw in the correct order. I have various components that create sprites attached to their corresponding nodes. There is no way to specify a layer for a node - only for Drawable2D objects. So I am setting a variable on the node called ‘layer’ and then retrieving that from within the component to set the correct layer for the Drawable2D objects.

For components with multiple sprites that must be layered - I am using ‘orderInLayer’ to arrange their relative order to the layer they are in.

Does this seem like the right approach?


Hey, I am having some trouble following, do you have this working and need a sanity check or having trouble getting it working? If the former, can I see a snippet of the creation logic here, I am confused on why layer is difficult to set on the Drawable2D at the given code context.


I think I have a handle on it now. The thing that was really confusing me was that Nodes didn’t have layers. But I realized I can just write a function to recursively go down through all the Drawables attached to a node and change their layer.

static void SetNodeLayer(SharedPtr<Node> node, int layer)
    PODVector <StaticSprite2D*> sprites;
    node->GetComponents<StaticSprite2D>(sprites, true);
    for (int i=0;i<sprites.Size();i++)

So effectively, I can act as if a node has a layer by assigning all the sprites under that node to the same layer. And if there is layering that needs to happen within those sprites - I’ll use the SetOrderInLayer to arrange them. I think that will handle all the cases I have.


Looks good here, though one note on passing SharedPtr by value, that is creating a SharedPtr container, with construction/destruction per call, to effectively increment and then decrement the internal ref count

I’d suggest passing by raw pointer or if SharedPtr is a must for some reason to pass the SharedPtr by reference


Ah good call - no reason that can’t be a pass by reference. Thanks!