diff --git a/cocos/2d/CCNode.cpp b/cocos/2d/CCNode.cpp index 70240cc3de..2a52bd1a2d 100644 --- a/cocos/2d/CCNode.cpp +++ b/cocos/2d/CCNode.cpp @@ -842,23 +842,13 @@ void Node::enumerateChildren(const std::string &name, std::function 2 && name[0] == '/' && name[1] == '/') { - if (length > 2 && name[1] == '/') - { - searchFromRootRecursive = true; - subStrStartPos = 2; - subStrlength -= 2; - } - else - { - searchFromRoot = true; - subStrStartPos = 1; - subStrlength -= 1; - } + searchRecursively = true; + subStrStartPos = 2; + subStrlength -= 2; } // End with '/..'? @@ -872,7 +862,7 @@ void Node::enumerateChildren(const std::string &name, std::functiondoEnumerate(newName, callback); - } - } - else if (searchFromRootRecursive) + + if (searchRecursively) { // name is '//xxx' - auto root = getScene(); - if (root) - { - doEnumerateRecursive(root, newName, callback); - } + doEnumerateRecursive(this, newName, callback); } else { diff --git a/cocos/2d/CCNode.h b/cocos/2d/CCNode.h index 80007d7758..7b26969cea 100644 --- a/cocos/2d/CCNode.h +++ b/cocos/2d/CCNode.h @@ -714,23 +714,19 @@ public: virtual Node* getChildByName(const std::string& name) const; /** Search the children of the receiving node to perform processing for nodes which share a name. * - * @param name The name to search for, supports c++11 regular expression + * @param name The name to search for, supports c++11 regular expression. * Search syntax options: - * `/` : When placed at the start of the search string, this indicates that the search should be performed on the tree's node. - * `//`: Can only be placed at the begin of the search string. This indicates that the search should be performed on the tree's node - * and be performed recursively across the entire node tree. + * `//`: Can only be placed at the begin of the search string. This indicates that it will search recursively. * `..`: The search should move up to the node's parent. Can only be placed at the end of string - * `/` : When placed anywhere but the start of the search string, this indicates that the search should move to the node's children + * `/` : When placed anywhere but the start of the search string, this indicates that the search should move to the node's children. * * @code - * enumerateChildren("/MyName", ...): This searches the root's children and matches any node with the name `MyName`. - * enumerateChildren("//MyName", ...): This searches the root's children recursively and matches any node with the name `MyName`. + * enumerateChildren("//MyName", ...): This searches the children recursively and matches any node with the name `MyName`. * enumerateChildren("[[:alnum:]]+", ...): This search string matches every node of its children. - * enumerateChildren("/MyName", ...): This searches the node tree and matches the parent node of every node named `MyName`. * enumerateChildren("A[[:digit:]]", ...): This searches the node's children and returns any child named `A0`, `A1`, ..., `A9` * enumerateChildren("Abby/Normal", ...): This searches the node's grandchildren and returns any node whose name is `Normal` * and whose parent is named `Abby`. - * enumerateChildren("//Abby/Normal", ...): This searches the node tree and returns any node whose name is `Normal` and whose + * enumerateChildren("//Abby/Normal", ...): This searches recursively and returns any node whose name is `Normal` and whose * parent is named `Abby`. * @endcode * diff --git a/tests/cpp-tests/Classes/NodeTest/NodeTest.cpp b/tests/cpp-tests/Classes/NodeTest/NodeTest.cpp index 1ad294deec..173a506ee6 100644 --- a/tests/cpp-tests/Classes/NodeTest/NodeTest.cpp +++ b/tests/cpp-tests/Classes/NodeTest/NodeTest.cpp @@ -1379,8 +1379,8 @@ void NodeNameTest::test(float dt) }); CCAssert(i == 10000, ""); - // name = /xxx : search from root - parent = getScene(); + // name = //xxx : search recursively + parent = Node::create(); for (int j = 0; j < 100; j++) { auto node = Node::create(); @@ -1398,35 +1398,21 @@ void NodeNameTest::test(float dt) } i = 0; - enumerateChildren("/node[[:digit:]]+", [&i](Node* node) -> bool { - ++i; - return false; - }); - CCAssert(i == 100, ""); - - i = 0; - enumerateChildren("/node[[:digit:]]+", [&i](Node* node) -> bool { - ++i; - return true; - }); - CCAssert(i == 1, ""); - - i = 0; - enumerateChildren("//node[[:digit:]]+", [&i](Node* node) -> bool { + parent->enumerateChildren("//node[[:digit:]]+", [&i](Node* node) -> bool { ++i; return false; }); CCAssert(i == 10100, ""); // 10000(children) + 100(parent) i = 0; - enumerateChildren("//node[[:digit:]]+", [&i](Node* node) -> bool { + parent->enumerateChildren("//node[[:digit:]]+", [&i](Node* node) -> bool { ++i; return true; }); CCAssert(i == 1, ""); i = 0; - enumerateChildren("//node[[:digit:]]+/..", [&i](Node* node) -> bool { + parent->enumerateChildren("//node[[:digit:]]+/..", [&i](Node* node) -> bool { ++i; return false; });