RecommendGamesNode.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import GamePage from "./GamePage";
  2. import MoreGamesPanel from "./MoreGamesPanel";
  3. import { utils } from "./Utils";
  4. import { SubLocation } from "./YZ_Constant";
  5. import PlatUtils from "./PlatUtils";
  6. const { ccclass, property } = cc._decorator;
  7. @ccclass
  8. export default class RecommendGamesNode extends cc.Component {
  9. @property(cc.Prefab)
  10. prefab: cc.Prefab = null;
  11. @property(cc.Prefab)
  12. prefab1: cc.Prefab = null;
  13. moreGamesPanel: MoreGamesPanel = null;
  14. _pageView: cc.PageView = null;
  15. _content: cc.Node = null;
  16. _gamePageNode: cc.Node = null;
  17. _dataDirty: boolean = false;
  18. _isContentFilled: boolean = false;
  19. _scrollInterval: number = 3;
  20. _timeTmp: number = 0;
  21. _gameList: any = null;
  22. moreGame: cc.Node = null;
  23. onLoad() {
  24. let pageViewNode: cc.Node = this.node.getChildByName("PageView");
  25. this.moreGame = cc.find("bg/BtnMore", this.node);
  26. this._pageView = pageViewNode.getComponent(cc.PageView);
  27. this._content = this._pageView.content;
  28. this._gamePageNode = this._content.getChildByName("GamePage");
  29. this._content.removeAllChildren();
  30. }
  31. public init(data: any) {
  32. this._gameList = data;
  33. if (this._gameList && this._gameList.length > 0) {
  34. this._dataDirty = true;
  35. } else {
  36. this.node.active = false;
  37. }
  38. }
  39. onEnable() {
  40. let self = this;
  41. this.moreGame.on(cc.Node.EventType.TOUCH_START, (event) => {
  42. if (PlatUtils.IsDouyin) {
  43. utils.Tool_Douyin.showMoreGamesModal();
  44. } else if (!PlatUtils.IsOPPO || (utils.ServerConfig.recommend_bar_show_pannel && utils.ServerConfig.recommend_bar_show_pannel == "true")) {
  45. let panel;
  46. if (utils.ServerConfig.more_game_skin == 2) {
  47. panel = cc.instantiate(self.prefab1);
  48. panel.zIndex = 999;
  49. self.moreGamesPanel = panel.getComponent("MoreGamesPanel1");
  50. } else {
  51. panel = cc.instantiate(self.prefab);
  52. panel.zIndex = 999;
  53. self.moreGamesPanel = panel.getComponent("MoreGamesPanel");
  54. }
  55. cc.director.getScene().addChild(panel);
  56. self.moreGamesPanel._location = SubLocation.isScrollbar;
  57. self.moreGamesPanel.init(self._gameList);
  58. self.moreGamesPanel.show();
  59. } else {
  60. utils.showLog("服务器未配置显示更多游戏面板!");
  61. }
  62. });
  63. }
  64. onDisable() {
  65. this.moreGame.targetOff(this);
  66. }
  67. update(dt: number) {
  68. if (this._dataDirty) {
  69. this._dataDirty = false;
  70. this._updateContent();
  71. }
  72. }
  73. _updateContent() {
  74. if (this._gameList) {
  75. utils.postRecommentShowData(SubLocation.isScrollbar);
  76. let length: number = Math.floor(this._gameList.length / 4);
  77. let index = 0;
  78. for (let i = 0; i < length; i++) {
  79. let gamePageList = [];
  80. for (let j = 0; j < 4; j++) {
  81. gamePageList.push(this._gameList[index]);
  82. index++;
  83. }
  84. let gamePageNode = cc.instantiate(this._gamePageNode);
  85. let gamePage: GamePage = gamePageNode.getComponent("GamePage");
  86. gamePage.init(gamePageList);
  87. this._pageView.addPage(gamePageNode);
  88. }
  89. this._gamePageNode.destroy();
  90. this._isContentFilled = true;
  91. this.autoRefrshPageView();
  92. }
  93. }
  94. autoRefrshPageView() {
  95. this.unscheduleAllCallbacks();
  96. let interval = 3.5;
  97. this.schedule(() => {
  98. let count = this._pageView.getPages().length;
  99. let index = this._pageView.getCurrentPageIndex();
  100. index = ((index < count) && (index + 1 !== count)) ? (index + 1) : 0;
  101. if (index == 0) {
  102. this._pageView.scrollToPage(index, 0);
  103. } else {
  104. this._pageView.scrollToPage(index, 2.5);
  105. }
  106. }, interval); //10秒一换
  107. }
  108. }