FlyUp/assets/Script/ListView/ItemRender.ts
2024-08-02 17:23:53 +08:00

56 lines
2.1 KiB
TypeScript

import List, { ListType } from "./List";
const { ccclass, property } = cc._decorator;
@ccclass
export default class ItemRender extends cc.Component {
/**数据 */
public data:any = null;
/**索引 0表示第一项*/
public itemIndex:number = 0;
/**数据改变时调用 */
public dataChanged(){
if(this.data.name.length > 7){
this.data.name = this.data.name.substring(0,7) + "..."
}
this.node.getChildByName("rankLab").getComponent(cc.Label).string = this.data.rank + "";
this.node.getChildByName("nameLab").getComponent(cc.Label).string = this.data.name + "";
this.node.getChildByName("totalLab").getComponent(cc.Label).string = this.data.total + "";
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;
}
this.setPic();
}
public setPic(){
let url = this.data.pic;
cc.assetManager.loadRemote(url, {ext:'.png'},(err, texture:cc.Texture2D) => {
if(texture){
var sprite = this.node.getChildByName("pic").getChildByName("pic").getComponent(cc.Sprite);
sprite.spriteFrame = new cc.SpriteFrame(texture);
// console.log("设置头像成功");
}
else{
// console.log(err,texture)
}
})
}
}