How do i search for a node that is present in the game scene?
in Unity we can use GameObject.find("object name"); there is one in cocos creator cc.find(path); but in my case i dont know where path is… so my node might be in the root scene or the node might be inside of another node
Straight forward approach would be to manually search the scene, something like this:
var findDeep = function(node, nodeName) {
if(node.name == nodeName) return node;
for(var i = 0; i < node.children.length; i++) {
var res = findDeep(node.children[i], nodeName);
if(res) return res;
}
}
findDeep( cc.director.getScene(), "nodeName" );
But it may be too slow to do it in runtime on bigger scenes, so you better simply know your node’s path. Probably there’s a better way, but I can’t seem to find any
4 Likes