diff --git a/cocos/scene-graph/node.ts b/cocos/scene-graph/node.ts index 421e1a3f827..eebc530744b 100644 --- a/cocos/scene-graph/node.ts +++ b/cocos/scene-graph/node.ts @@ -630,7 +630,7 @@ export class Node extends CCObject implements ISchedulable, CustomSerializable { return; } const siblings = this._parent._children; - index = index !== -1 ? index : siblings.length - 1; + index = index >= 0 ? index : siblings.length + index; const oldIndex = siblings.indexOf(this); if (index !== oldIndex) { siblings.splice(oldIndex, 1); diff --git a/native/cocos/core/scene-graph/Node.cpp b/native/cocos/core/scene-graph/Node.cpp index bc84ecf0d26..5ec8550dd2d 100644 --- a/native/cocos/core/scene-graph/Node.cpp +++ b/native/cocos/core/scene-graph/Node.cpp @@ -353,7 +353,8 @@ void Node::setSiblingIndex(index_t index) { return; } ccstd::vector> &siblings = _parent->_children; - index = index != -1 ? index : static_cast(siblings.size()) - 1; + index = index >= 0 ? index : static_cast(siblings.size()) + index; + index = index >= 0 ? index : 0; index_t oldIdx = getIdxOfChild(siblings, this); if (index != oldIdx) { if (oldIdx != CC_INVALID_INDEX) {