add utils::findChildren()

This commit is contained in:
minggo 2014-06-26 14:05:30 +08:00
parent eb67d9deff
commit 2917596d5e
3 changed files with 33 additions and 15 deletions

View File

@ -117,6 +117,19 @@ void captureScreen(const std::function<void(bool, const std::string&)>& afterCap
captureScreenCommand.func = std::bind(onCaptureScreen, afterCaptured, filename);
Director::getInstance()->getRenderer()->addCommand(&captureScreenCommand);
}
std::vector<Node*> findChildren(const Node &node, const std::string &name)
{
std::vector<Node*> vec;
node.enumerateChildren(name, [&vec](Node* nodeFound) -> bool {
vec.push_back(nodeFound);
return false;
});
return vec;
}
}
NS_CC_END

View File

@ -24,6 +24,10 @@ THE SOFTWARE.
****************************************************************************/
#ifndef __SUPPORT_CC_UTILS_H__
#define __SUPPORT_CC_UTILS_H__
#include <vector>
#include <string>
#include "2d/CCNode.h"
#include "base/ccMacros.h"
/** @file ccUtils.h
@ -58,6 +62,8 @@ namespace utils
* @since v3.2
*/
void captureScreen(const std::function<void(bool, const std::string&)>& afterCaptured, const std::string& filename);
std::vector<Node*> findChildren(const Node &node, const std::string &name);
}
NS_CC_END

View File

@ -1266,23 +1266,9 @@ void NodeNameTest::onEnter()
log("find child: %s", node->getName().c_str());
}
// enumerateChildren()
int i = 0;
parent->enumerateChildren("test", [&i](Node* node) -> bool {
++i;
return true;
});
CCAssert(i == 1, "");
i = 0;
parent->enumerateChildren("test", [&i](Node* node) -> bool {
++i;
return false;
});
CCAssert(i == 2, "");
// enumerateChildren()
// name = regular expression
int i = 0;
parent = Node::create();
for (int i = 0; i < 100; ++i)
{
@ -1422,6 +1408,19 @@ void NodeNameTest::onEnter()
return false;
});
CCAssert(i == 10000, "");
// utils::findChildren()
parent = Node::create();
for (int i = 0; i < 50; ++i)
{
auto child = Node::create();
child->setName("node");
parent->addChild(child);
}
auto findChildren = utils::findChildren(*parent, "node");
CCAssert(findChildren.size() == 50, "");
}
///