import RoundBox from "../../Script/module/Tool/RoundBox"; import NumberToImage from "../../Script/NumberToImage"; const { ccclass, property } = cc._decorator; @ccclass export default class CareerItem2 extends cc.Component { /**数据 */ public data: any = null; /**索引 0表示第一项*/ public itemIndex: number = 0; @property(cc.SpriteFrame) defaultsprite: cc.SpriteFrame = null; @property(cc.SpriteAtlas) fontUI: cc.SpriteAtlas = null; @property(cc.SpriteAtlas) UI: cc.SpriteAtlas = null; @property(cc.SpriteAtlas) UI2: cc.SpriteAtlas = null; randerChildren: any; @property(cc.SpriteAtlas) catUI: cc.SpriteAtlas = null; @property(cc.Prefab) iconPrefab: cc.Prefab = null; /** Stable references that are not affected when zIndex reorders node.children. */ private rankingNodes: cc.Node[] = []; private childBaseScales: { [index: number]: number } = {}; private readonly childEnterDuration: number = 0.04; /** * Cache the prefab-authored order once. Pooled CareerItem2 nodes keep these * children, so the references remain valid for the component's lifetime. */ private cacheRankingNodes() { if (this.rankingNodes.length > 0) { return; } const length = Math.min(32, this.node.children.length); for (let i = 8; i < length; i++) { this.rankingNodes.push(this.node.children[i]); } } private getRankingNode(childIndex: number): cc.Node { return this.rankingNodes[childIndex - 8]; } /** 根据固定的预制体节点顺序,取得被点击 careerCityItem 对应的用户数据。 */ public getRankingDataByNode(target: cc.Node): any { if (!target) { return null; } this.cacheRankingNodes(); const dataIndex = this.rankingNodes.indexOf(target); const rankingData = this.data && this.data.rankingData; return Array.isArray(rankingData) && dataIndex >= 0 && dataIndex < rankingData.length ? rankingData[dataIndex] : null; } /** * Stop animations left by a recycled list item and hide all ranking nodes. */ private resetChildrenEnterState() { const length = this.rankingNodes.length + 8; for (let i = 8; i < length; i++) { const child = this.getRankingNode(i); cc.Tween.stopAllByTarget(child); if (this.childBaseScales[i] === undefined) { this.childBaseScales[i] = child.scale; } child.opacity = 0; child.scale = this.childBaseScales[i] * 0.8; } } /** * 8-17 enter one by one. After 17, 18/19, 20/21 ... 30/31 enter in pairs. */ private playChildrenEnterAnimation(length: number) { for (let i = 8; i < length; i++) { const child = this.getRankingNode(i); const groupIndex = i <= 17 ? i - 8 : 10 + Math.floor((i - 18) / 2); cc.tween(child) .delay(groupIndex * this.childEnterDuration) .to(this.childEnterDuration, { opacity: 255, scale: this.childBaseScales[i] }, { easing: "backOut" }) .start(); } } /**数据改变时调用 */ public dataChanged() { this.randerChildren = []; this.cacheRankingNodes(); this.resetChildrenEnterState(); const rankingNodeEndIndex = this.rankingNodes.length + 8; for (let i = 8; i < rankingNodeEndIndex; i++) { const child = this.getRankingNode(i); // this.node.children[i].getChildByName("name").getComponent(cc.Label).string = ""; if (child.getChildByName("level")) child.getChildByName("level").getComponent(cc.Label).string = ""; child.getChildByName("mask").getChildByName("icon").getComponent(cc.Sprite).spriteFrame = null; child.zIndex = 1000 - Math.abs(child.x); // this.node.children[i].getChildByName("kong").active = true; } if (this.data != undefined) { this.node.opacity = 255; // this.node.getChildByName("rank").getComponent(cc.Label).string = this.data.count + ""; this.node.getChildByName("cityName").getComponent(cc.Sprite).spriteFrame = this.UI2.getSpriteFrame(this.data.name); this.node.getChildByName("ranking").getChildByName("one").active = false; this.node.getChildByName("ranking").getChildByName("two").active = false; this.node.getChildByName("ranking").getChildByName("three").active = false; this.node.getChildByName("ranking").getChildByName("nomal").active = false; if (this.data.rank == 1) { this.node.getChildByName("ranking").getChildByName("one").active = true; this.node.getChildByName("bg").getChildByName("bg1").active = false; NumberToImage.numberToImageNodesPublic(this.fontUI, this.data.count, 25, 30, "cat", this.node.getChildByName("rank"), false); } else if (this.data.rank == 2) { this.node.getChildByName("ranking").getChildByName("two").active = true; this.node.getChildByName("bg").getChildByName("bg1").active = false; NumberToImage.numberToImageNodesPublic(this.fontUI, this.data.count, 25, 30, "cat", this.node.getChildByName("rank"), false); } else if (this.data.rank == 3) { this.node.getChildByName("ranking").getChildByName("three").active = true; this.node.getChildByName("bg").getChildByName("bg1").active = false; NumberToImage.numberToImageNodesPublic(this.fontUI, this.data.count, 25, 30, "cat", this.node.getChildByName("rank"), false); } else { NumberToImage.numberToImageNodesPublic(this.fontUI, this.data.count, 25, 30, "cat", this.node.getChildByName("rank"), false); this.node.getChildByName("bg").getChildByName("bg1").active = false; this.node.getChildByName("ranking").getChildByName("nomal").active = true; NumberToImage.numberToImageNodes4(this.data.rank, 30, 15, "company_", this.node.getChildByName("ranking").getChildByName("nomal").getChildByName("rank"), true); let rank = this.node.getChildByName("ranking").getChildByName("nomal").getChildByName("rank"); if (rank && rank.children) { if (rank.children.length > 0) { for (let i = 0; i < rank.children.length; i++) { rank.children[i].color = cc.Color.WHITE; } } } } this.node.zIndex = this.data.rank; if (this.randerChildren) { if (this.randerChildren.length == 0) { this.randerChildren = []; let lengthTemp = 0; if (this.data.rankingData) { if (this.data.rankingData.length) { lengthTemp = this.data.rankingData.length; } } let length = lengthTemp + 8; if (length > rankingNodeEndIndex) length = rankingNodeEndIndex; for (let i = 8; i < length; i++) { const child = this.getRankingNode(i); this.randerChildren.push(child); let username = cc.fx.GameTool.subName(this.data.rankingData[i - 8].username, 5); if (username == "user") username = "匿名玩家"; // console.log("用户名字:", username); // this.node.children[i].getChildByName("kong").active = false; // this.node.children[i].getChildByName("name").getComponent(cc.Label).string = username + ""; if (this.data.rankingData[i - 8].userCat != undefined && this.data.rankingData[i - 8].userCat != "" && this.data.rankingData[i - 8].userCat != null) { if (parseInt(this.data.rankingData[i - 8].userCat) < 12 || parseInt(this.data.rankingData[i - 8].userCat) > 0) { child.getChildByName("icon").active = true; child.getChildByName("icon").getComponent(cc.Sprite).spriteFrame = this.catUI.getSpriteFrame("cat" + this.data.rankingData[i - 8].userCat); } } if (child.getChildByName("level")) child.getChildByName("level").getComponent(cc.Label).string = this.data.rankingData[i - 8].addLevel; if (this.data.rankingData[i - 8].useravatar == "" || this.data.rankingData[i - 8].useravatar == null || this.data.rankingData[i - 8].useravatar == undefined ) { // this.firstRender.children[i].getChildByName("icon").getComponent(cc.Sprite).spriteFrame = this.defaultsprite; } else if (this.data.rankingData[i - 8].useravatar == "0" || this.data.rankingData[i - 8].useravatar == "1" || this.data.rankingData[i - 8].useravatar == "2" || this.data.rankingData[i - 8].useravatar == "3" || this.data.rankingData[i - 8].useravatar == "4" || this.data.rankingData[i - 8].useravatar == "5" || this.data.rankingData[i - 8].useravatar == "6" || this.data.rankingData[i - 8].useravatar == "7" || this.data.rankingData[i - 8].useravatar == "8" || this.data.rankingData[i - 8].useravatar == "9" || this.data.rankingData[i - 8].useravatar == "10" || this.data.rankingData[i - 8].useravatar == "11" || this.data.rankingData[i - 8].useravatar == "12" || this.data.rankingData[i - 8].useravatar == "13" || this.data.rankingData[i - 8].useravatar == "14" || this.data.rankingData[i - 8].useravatar == "15" || this.data.rankingData[i - 8].useravatar == "16" || this.data.rankingData[i - 8].useravatar == "17" || this.data.rankingData[i - 8].useravatar == "18" || this.data.rankingData[i - 8].useravatar == "19" || this.data.rankingData[i - 8].useravatar == "20" || this.data.rankingData[i - 8].useravatar == "21" || this.data.rankingData[i - 8].useravatar == "22" || this.data.rankingData[i - 8].useravatar == "23" || this.data.rankingData[i - 8].useravatar == "24" || this.data.rankingData[i - 8].useravatar == "25" || this.data.rankingData[i - 8].useravatar == "26" ) { child.getChildByName("mask").getChildByName("icon").active = false; let useravatar = this.data.rankingData[i - 8].useravatar; let actionName = cc.fx.GameTool.getActionName(useravatar); child.getChildByName("mask").getChildByName("sp").active = true; child.getChildByName("mask").getChildByName("sp").scale = 0.23; cc.fx.GameTool.loadSpineSimple(child.getChildByName("mask").getChildByName("sp"), actionName); // let useravatar = this.data.rankingData[i - 8].useravatar; // let useravatarTemp = "icon_" + useravatar; // this.node.children[i].getChildByName("mask").getChildByName("icon").getComponent(cc.Sprite).spriteFrame = this.UI.getSpriteFrame(useravatarTemp); } else { child.getChildByName("mask").getChildByName("icon").active = true; child.getChildByName("mask").getChildByName("sp").active = false; this.setPic(this.data.rankingData[i - 8].useravatar, child.getChildByName("mask").getChildByName("icon")); } } this.playChildrenEnterAnimation(length); } } } else { this.node.opacity = 0; } } public setPic(url, node) { // 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 && node) { // this.node.getChildByName("pic").active = true; var sprite = node.getComponent(cc.Sprite); sprite.spriteFrame = new cc.SpriteFrame(texture); } else { // console.log("加载图片失败" + url); } }) } }