BaseUI.js 1.94 KB
Newer Older
asdf's avatar
.  
asdf committed
1 2 3
cc.Class({
    extends: cc.Component,
    
asdf's avatar
.  
asdf committed
4
    onLoad() {
asdf's avatar
.  
asdf committed
5
        this.nodeDict = {};
asdf's avatar
.  
asdf committed
6
        this.linkWidget(this.node);
asdf's avatar
.  
asdf committed
7
    },
asdf's avatar
.  
asdf committed
8 9 10 11 12 13

    // 遍历节点树,获取重要节点
    // 节点名字以$开头的节点为重要节点,放进nodeDict中,可以直接拿到,不用拖拽绑定
    // $btn为按钮类型,放进nodeDict,并且绑定按钮点击事件
    // $ui为ui节点,放进nodeDict,并且上面绑定了BaseUI脚本,所以不继续遍历该节点的子节点
    // $btnUI为按钮类型ui节点,放进nodeDict,并且上面绑定了BaseUI脚本,所以不继续遍历该节点的子节点,并且绑定按钮点击事件
asdf's avatar
.  
asdf committed
14
    linkWidget(node) {
asdf's avatar
.  
asdf committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
        let children = node.children;
        for (let i = 0; i < children.length; i++) {
            let nodeName = children[i].name;
            if (nodeName.substring(0, 1) !== "$") {
                this.linkWidget(children[i]);
                continue;
            }

            let realName = nodeName.substring(1);
            if (this.nodeDict[realName]) {
                cc.error("节点名字重复!" + realName);
                this.linkWidget(children[i]);
                continue;
            }
            let isUI = false;
            this.nodeDict[realName] = children[i];
            if (nodeName.substring(1, 6) === "btnUI") {
                children[i].on('click', this.buttonListener, this);
                isUI = true;
            } else if (nodeName.substring(1, 4) === "btn") {
                children[i].on('click', this.buttonListener, this);
                isUI = false;
            } else if (nodeName.substring(1, 3) === "ui") {
                isUI = true;
            }
            children[i].name = realName;
            this.nodeDict[realName] = children[i];

            if (!isUI) {
                this.linkWidget(children[i]);
            }
        }
asdf's avatar
.  
asdf committed
47
    },
asdf's avatar
.  
asdf committed
48

asdf's avatar
.  
asdf committed
49
    getNodeByName(name) {
asdf's avatar
.  
asdf committed
50
        return this.nodeDict[name];
asdf's avatar
.  
asdf committed
51
    },
asdf's avatar
.  
asdf committed
52

asdf's avatar
.  
asdf committed
53 54
    buttonListener(button) { }
});