Nanfeng

Notes on software development, code, and curious ideas

Changing Node Order with setSiblingIndex in Cocos Creator

To change a node’s position among its siblings in Cocos Creator, use setSiblingIndex. Reordering siblings also changes their relative rendering order where sibling order controls drawing.

Suppose you want to move one node immediately after a target node:

1
2
3
4
5
const nodeToMove = cc.find("NodeToMove");
const targetNode = cc.find("TargetNode");

const targetIndex = targetNode.getSiblingIndex();
nodeToMove.setSiblingIndex(targetIndex + 1);

The code obtains references to both nodes, reads the target’s current index with getSiblingIndex(), and assigns the moving node the following index.

Replace NodeToMove and TargetNode with the paths or names used in your scene. Both nodes must share the same parent for sibling-index ordering to have the intended effect, and you should also guard against either lookup returning null in production code.

+