cocos2d-js ccui seekWidgetByName() 函数的改进

ccui.helper.seekWidgetByName(root, name) 查找节点的方式深度优先查找的方式,而且只能传节点的名字不能传路径,效率不高,稍微改进了一下,新的 seekWidgetByPath 可根据路径查找。

比如 var node = ccui.helper.seekWidgetByName(root, "LabelName") 可以精确地写成 var node = GameClient.CocosHelper.seekWidgetByPath(root, "CorpsPanel/Toggle/Num/LabelName")

代码如下

// 单例
GameClient.CocosHelper = {
    /**
     * Finds a widget whose name equals to param name from root widget. 广度优先查找
     * @param {ccui.Widget} root
     * @param {String} path, 支持比如 CorpsPanel/Toggle/Num 这种方式的查找
     * @returns {ccui.Widget}
     */
    seekWidgetByPath: function (root, path) {
        if (root == null) {
            cc.warn("error, seekWidgetByPath(), root is null, path: " + String(path));
            return null;
        }

//        cc.warn("root: " + root.getName());

        var strList = path.split("/");
        var strListLength = strList.length;
        var nextNode = root; // 在当前节点的子节点下查找

        for (var i = 0; i < strListLength; i++) {
            var childName = strList[i]; // 当前层级下要找的子节点名字
            var arrayRootChildren = nextNode.getChildren(); // 得到左右子节点
            var length = arrayRootChildren.length;
            var foundNext = false;
//            cc.warn(i + ", childName: " + childName);
            for (var j = 0; j < length; j++) {
                var childNode = arrayRootChildren[j];
//                cc.warn(i + ", childNode: " + childNode.getName());
                // 找到了这个子节点
                if (childNode.getName() == childName) {
                    // 如果是最后一个子节点说明找到了
                    if (i == strListLength - 1) {
                        return childNode;
                    }
                    else {
                        foundNext = true;
                        nextNode = childNode;
                        break;
                    }
                }
            }

            if (!foundNext) {
                return null;
            }
        }

        return null;
    },

    FuncEmpty: function () {

    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值