group.js 1.72 KB
Newer Older
linzhiguo's avatar
linzhiguo committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
cc.Class({
    extends: cc.Component,
    name: "KindGroup",
    properties: {
        template_node:{
            default:null,
            type: cc.Node,
        },

        layout:{
            default:null,
            type: cc.Layout
        }
    },

    init(data){
        this.node.removeAllChildren();
        let item, kind, len = data.length;
        this._num = len;
        for(let i = 0; i < len; i++){
            item = cc.instantiate(this.template_node);
            item.y = 0;
            this.node.addChild(item);
            kind = item.getComponent(cc.js.getClassByName('WordKind'));
            kind.init(data[i]);
            console.log(i);
            item.y += 43;
        }

        this.updateView();
    },

    updateView(){
        let sw = this.node.width;
        let wr = sw/1280;
        let num = this.node.childrenCount;
        let width = -1;
        if (num== 2){
            this.layout.paddingLeft = 100;
            this.layout.spacingX = 100;
            width = 479;
        }
        else if (num== 3){
            this.layout.paddingLeft = 45;
            this.layout.spacingX = 45;
            width = 368;
        }
        else if (num== 4){
            this.layout.paddingLeft = 45;
            this.layout.spacingX = 30;
            width = 273;
        }
        else {
            this.layout.paddingLeft = 10;
            this.layout.spacingX = 10;
            width = (1280-num*10-10) / num;

        }

        this.layout.paddingLeft *= wr;
        this.layout.spacingX *= wr;
        width = width * wr;

        // width = Math.max(100, width);
        // width = Math.min(479, width);


        for(let i = 0; i < num; i++){
            this.node.children[i].width = width;
        }

    }
});