

cc.Class({
    extends: cc.Component,

    properties: {
        rankSprite:[cc.SpriteFrame],  //名次图片(第1，第2，第3名)精灵帧
        spMingci: cc.Sprite,  //名次图片精灵
        labmingci: cc.Label,  //名次文字
        labName: cc.Label,  //玩家名字
        labScore: cc.Label,  //玩家分数
        spdoufuHead: cc.Sprite,  //玩家头像
        spBg: cc.Node,  //名次底图  
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {

    },

    init: function(rank,data){
        // console.log("结束排行",rank,data);
        var nick = data.nickname;
        var grade = data.KVDataList.length != 0 ? data.KVDataList[0].value : 0;
        var avatarUrl = data.avatarUrl;
        this.labName.string = nick;  //显示玩家名字
        this.labScore.string = "第"+grade+"关";  //显示玩家分数

        if(rank ==0){
            this.spMingci.spriteFrame =this.rankSprite[0];
        }else if(rank ==1){
            this.spMingci.spriteFrame =this.rankSprite[1];
        }else if(rank ==2){
            this.spMingci.spriteFrame =this.rankSprite[2];    
        }else if(rank =="暂无排名"){
            this.spMingci.spriteFrame =null;    
        }else{
            this.spMingci.node.active =false;  //名次图片隐藏
            this.labmingci.node.active =true;  //名次数字显示
            this.labmingci.string ="第"+(rank+1)+"名";
        }  
        
        //显示用户头像的图片
        this.createImage(avatarUrl);
    },

    createImage: function(avatarUrl){
        // console.log("路径",avatarUrl);
        if (CC_WECHATGAME) {
            try {
                let image = wx.createImage();
                image.onload = () => {
                    try {
                        let texture = new cc.Texture2D();
                        texture.initWithElement(image);
                        texture.handleLoadedTexture();
                        this.spdoufuHead.spriteFrame = new cc.SpriteFrame(texture);
                    } catch (e) {
                        cc.log(e);
                        this.spdoufuHead.node.active = false;
                    }
                };
                image.src = avatarUrl;
            }catch (e) {
                cc.log(e);
                this.spdoufuHead.node.active = false;
            }
        } else {
            cc.loader.load({
                url: avatarUrl, type: 'jpg'
            }, (err, texture) => {
                this.spdoufuHead.spriteFrame = new cc.SpriteFrame(texture);
            });
        } 
    },

    //清空预制里的子节点
    onClear: function(){
        this.spBg.active =false;
        // this.defenkuang.active =false;
        this.spdoufuHead.node.active =false;
        this.labScore.node.active =false;
        this.labName.node.active =false;
    },

    // update (dt) {},
});
