Help.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { _decorator, assetManager, Button, Component, ImageAsset, Node, PageView, resources, Sprite, SpriteAtlas, SpriteFrame, Toggle } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass('Help')
  4. export class Help extends Component {
  5. @property(Node)
  6. img:any = null;
  7. pageViewCom:PageView = null;
  8. totalPages:number = null;
  9. currentPageIndex:number = null;
  10. test:number=null;
  11. protected onLoad(): void {
  12. }
  13. start() {
  14. // this.pageViewCom = this.node.getChildByName("HelpPageView").getComponent(PageView);
  15. // this.currentPageIndex = this.pageViewCom.getCurrentPageIndex();
  16. // this.totalPages = this.pageViewCom.getPages().length;
  17. // this.pageViewCom.node.on(PageView.EventType.PAGE_TURNING,this.onPageTurn,this);
  18. //this.onScrollPage('HelpPageView');
  19. const btnHome = this.node.getChildByName("Button");
  20. btnHome.on(Button.EventType.CLICK,()=>{
  21. if(this.node.active){
  22. this.node.active = false;
  23. }
  24. },this)
  25. }
  26. bottomCurrentIndex(pageViewName:string){
  27. this.pageViewCom = this.node.getChildByName(pageViewName).getComponent(PageView);
  28. this.currentPageIndex = this.pageViewCom.getCurrentPageIndex();
  29. this.loadRes();
  30. this.pageViewCom.node.on(PageView.EventType.PAGE_TURNING,this.onPageTurn,this);
  31. }
  32. onTotalPages(pageViewName:string){
  33. if(!pageViewName){
  34. pageViewName = 'HelpPageView';
  35. }
  36. const pageViewCom = this.node.getChildByName(pageViewName).getComponent(PageView);
  37. const totalPagesLength = pageViewCom.getPages().length;
  38. // 加载 SpriteAtlas(图集),并且获取其中的一个 SpriteFrame
  39. // 注意 atlas 资源文件(plist)通常会和一个同名的图片文件(png)放在一个目录下, 所以需要在第二个参数指定资源类型
  40. resources.load("number", SpriteAtlas, (err, atlas) => {
  41. const totalPages = this.node.getChildByPath('Bottom/totalPages');
  42. const totalPagesSprite = totalPages.getComponent(Sprite);
  43. const count:number = totalPagesLength + 48;
  44. const numTotalSprite = count.toString();
  45. totalPagesSprite.spriteFrame = atlas.getSpriteFrame('font_0' + numTotalSprite);
  46. });
  47. }
  48. onPageTurn(){
  49. this.currentPageIndex = this.pageViewCom.getCurrentPageIndex();
  50. this.loadRes();
  51. }
  52. // onPageTurn() {
  53. // // Load the sprite atlas and update the current index
  54. // this.loadSpriteAtlas("number").then(atlas => {
  55. // const currentIndex = this.node.getChildByPath('Bottom/currentIndex');
  56. // const currentIndexSprite = currentIndex.getComponent(Sprite);
  57. // const countIndex: number = this.currentPageIndex + 49;
  58. // const numIndex = countIndex.toString();
  59. // currentIndexSprite.spriteFrame = atlas.getSpriteFrame('font_0' + numIndex);
  60. // }).catch(err => {
  61. // console.error("Error loading sprite atlas:", err);
  62. // });
  63. // }
  64. //Helper function to load the sprite atlas
  65. // loadSpriteAtlas(atlasName: string): Promise<SpriteAtlas> {
  66. // return new Promise((resolve, reject) => {
  67. // resources.load(atlasName, SpriteAtlas, (err: Error, atlas) => {
  68. // if (err) {
  69. // reject(err);
  70. // } else {
  71. // resolve(atlas);
  72. // }
  73. // });
  74. // });
  75. // }
  76. loadRes(){
  77. resources.load("number",SpriteAtlas,(err:Error,atlas)=>{
  78. const currentIndex = this.node.getChildByPath('Bottom/currentIndex');
  79. const currentIndexSprite = currentIndex.getComponent(Sprite);
  80. const countIndex:number = this.currentPageIndex + 49;
  81. const numIndex = countIndex.toString();
  82. currentIndexSprite.spriteFrame = atlas.getSpriteFrame('font_0' + numIndex);
  83. })
  84. }
  85. isChecked(){
  86. const toggleGroup = this.node.getChildByName("ToggleGroup");
  87. const toggleNames:string[] = ['Help','Monster','Tower'];
  88. const pageViews:string[] = ['HelpPageView','HelpMonster','TowerPageView'];
  89. //const nodeToggleGroupChild:Node[] = [];
  90. toggleNames.forEach((name,index)=>{
  91. const toggle = toggleGroup.getChildByName(name).getComponent(Toggle);
  92. const pageView = this.node.getChildByName(pageViews[index]);
  93. pageView.active = toggle.isChecked;
  94. if(toggle.isChecked && pageView.name != "HelpMonster"){
  95. this.onTotalPages(pageView.name);
  96. this.bottomCurrentIndex(pageView.name);
  97. }
  98. })
  99. // for(let i = 0 ; i < arrToggle.length ; i++){
  100. // const nodeTG = toggleGroup.getChildByName(arrToggle[i])
  101. // nodeToggleGroupChild.push(nodeTG);
  102. // }
  103. // for(let i = 0; i < nodeToggleGroupChild.length; i++){
  104. // //let isActive:boolean = this.node.getChildByName(node[i]).active;
  105. // if(nodeToggleGroupChild[i].getComponent(Toggle).isChecked){
  106. // this.node.getChildByName(node[i]).active = true;
  107. // } else {
  108. // this.node.getChildByName(node[i]).active = false;
  109. // }
  110. // }
  111. // const help = toggleGroup.getChildByName("Help");
  112. // const monster = toggleGroup.getChildByName("Monster");
  113. // const tower = toggleGroup.getChildByName("Tower");
  114. // if(help.getComponent(Toggle).isChecked === true){
  115. // this.node.getChildByName('HelpPageView').active = true;
  116. // } else {
  117. // this.node.getChildByName('HelpPageView').active = false;
  118. // }
  119. // if(monster.getComponent(Toggle).isChecked === true){
  120. // this.node.getChildByName('Monster').active = true;
  121. // } else {
  122. // this.node.getChildByName("Monster").active = false;
  123. // }
  124. // if(tower.getComponent(Toggle).isChecked === true){
  125. // this.node.getChildByName('TowerPageView').active = true;
  126. // } else {
  127. // this.node.getChildByName('TowerPageView').active = false;
  128. // }
  129. }
  130. isNeedBottom(){
  131. if(this.node.getChildByName("HelpMonster").active){
  132. this.node.getChildByName('Bottom').active = false;
  133. } else {
  134. this.node.getChildByName('Bottom').active = true;
  135. }
  136. }
  137. update(deltaTime: number) {
  138. this.isNeedBottom();
  139. this.isChecked();
  140. this.currentPageIndex = this.pageViewCom.getCurrentPageIndex();
  141. }
  142. }