UIRank.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Learn TypeScript:
  2. // - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
  3. // Learn Attribute:
  4. // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
  5. // Learn life-cycle callbacks:
  6. // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
  7. import PrefabManager from "../Manager/core/PrefabManager";
  8. import {UIID} from "../Manager/core/UIID";
  9. import { gameModel } from "../Module/Module";
  10. import CCUtils from "../Utiles/CCUtils";
  11. import Utiles from "../Utiles/Utiles";
  12. const {ccclass, property} = cc._decorator;
  13. @ccclass
  14. export default class UIRank extends cc.Component {
  15. @property(cc.Sprite)
  16. closeBtn: cc.Sprite = null;
  17. // LIFE-CYCLE CALLBACKS:
  18. // onLoad () {}
  19. start () {
  20. }
  21. protected onEnable(): void {
  22. this.closeBtn.node.on('click',this.closeEvent,this)
  23. const content: cc.Node = CCUtils.findChild(this.node , 'tcBg/scrollView/view/content');
  24. content.removeAllChildren();
  25. // 获取数组中最大的值
  26. if (gameModel?.historyScores.length > 0) {
  27. const maxNum: number = gameModel.historyScores.reduce((a, b) => Math.max(a, b));
  28. console.log('maxNum', maxNum);
  29. CCUtils.findChild(this.node , 'tcBg/scrollView/view/content').height = gameModel.historyScores.length * 100 + 100;
  30. for (let i: number = gameModel.historyScores.length; i > 0; i--) {
  31. const node: cc.Node = cc.instantiate(CCUtils.findChild(this.node, 'tcBg/scoreItem'));
  32. node.parent = content;
  33. node.setPosition(0, -i * 100);
  34. CCUtils.getLabel(node, 'score').string = gameModel.historyScores[i - 1].toString();
  35. CCUtils.getLabel(node, 'name').string = Utiles.random(1, 20).toString();
  36. }
  37. CCUtils.getLabel(this.node, 'tcBg/goatLabel').string = 'Highest score: ' + maxNum.toString();
  38. }
  39. }
  40. protected onDisable(): void {
  41. this.closeBtn.node.off('click',this.closeEvent,this)
  42. }
  43. private closeEvent():void{
  44. console.log("=====closeEvent====")
  45. // this.node.destroy()
  46. PrefabManager.close(UIID.UIRankPrefab);
  47. }
  48. // update (dt) {}
  49. }