import NumberToImage from "../../NumberToImage"; const { ccclass, property } = cc._decorator; @ccclass export default class ItemRender extends cc.Component { /**数据 */ public data: any = null; /**索引 0表示第一项*/ public itemIndex: number = 0; @property(cc.SpriteFrame) defaultsprite: cc.SpriteFrame = null; @property(cc.SpriteAtlas) ui: cc.SpriteAtlas = null; /**数据改变时调用 */ public dataChanged() { let useravatarIcon = this.data.useravatarIcon; useravatarIcon = "kuang_" + (parseInt(useravatarIcon) + 1); this.node.getChildByName("icon").getComponent(cc.Sprite).spriteFrame = this.ui.getSpriteFrame(useravatarIcon); // console.log("用户头像框:", this.data.useravatarIcon, useravatarIcon); this.data.username = cc.fx.GameTool.subName(this.data.username, 7); let name = this.data.username; if (name == "user") name = "匿名玩家"; // this.node.getChildByName("rankLab").getComponent(cc.Label).string = this.data.rank + ""; this.node.getChildByName("nameLab").getComponent(cc.Label).string = name + ""; // this.node.getChildByName("totalLab").getComponent(cc.Label).string = this.data.levelAmount; NumberToImage.numberToImageNodes3((this.data.rank), 43, 15, "rank_", this.node.getChildByName("rankLab"), true); NumberToImage.numberToImageNodes3(this.data.levelAmount, 43, 15, "level_", this.node.getChildByName("totalLab"), true); // this.node.getChildByName("timeLab").getComponent(cc.Label).string = timeTemp + ""; this.node.getChildByName("rank").getChildByName("one").active = false; this.node.getChildByName("rank").getChildByName("two").active = false; this.node.getChildByName("rank").getChildByName("three").active = false; if (this.data.rank == 1) { this.node.getChildByName("rank").getChildByName("one").active = true; this.node.getChildByName("rankLab").active = false; } else if (this.data.rank == 2) { this.node.getChildByName("rank").getChildByName("two").active = true; this.node.getChildByName("rankLab").active = false; } else if (this.data.rank == 3) { this.node.getChildByName("rank").getChildByName("three").active = true; this.node.getChildByName("rankLab").active = false; } else { this.node.getChildByName("rankLab").active = true; } if (this.data.useravatar == "" || this.data.useravatar == null || this.data.useravatar == undefined ) { this.node.getChildByName("pic").getChildByName("pic").getComponent(cc.Sprite).spriteFrame = this.defaultsprite; } else if (this.data.useravatar == "0" || this.data.useravatar == "1" || this.data.useravatar == "2" || this.data.useravatar == "3" || this.data.useravatar == "4" || this.data.useravatar == "5" || this.data.useravatar == "6" || this.data.useravatar == "7" || this.data.useravatar == "8" || this.data.useravatar == "9" || this.data.useravatar == "10" || this.data.useravatar == "11" || this.data.useravatar == "12" || this.data.useravatar == "13" || this.data.useravatar == "14" || this.data.useravatar == "15" || this.data.useravatar == "16" || this.data.useravatar == "17" ) { let useravatar = this.data.useravatar; let useravatarTemp = "icon_" + useravatar; // console.log("用户头像:", useravatarTemp); this.node.getChildByName("pic").getChildByName("pic").getComponent(cc.Sprite).spriteFrame = this.ui.getSpriteFrame(useravatarTemp); } else this.setPic(); } public setPic() { // console.log("设置头像:", this.data.useravatar); // this.node.getChildByName("pic").getChildByName("icon").active = false; this.node.getChildByName("pic").active = false; var self = this; let url = this.data.useravatar; cc.assetManager.loadRemote(url, { ext: '.png' }, (err, texture: cc.Texture2D) => { if (texture) { this.node.getChildByName("pic").active = true; var sprite = this.node.getChildByName("pic").getChildByName("pic").getComponent(cc.Sprite); sprite.spriteFrame = new cc.SpriteFrame(texture); // console.log(this.data.rank,"设置头像成功",err); } else { // console.log("设置头像失败",url); // console.log(err, texture) } }) } }