NativeTryGameNode.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import { utils } from "./Utils";
  2. import PlatUtils from "./PlatUtils";
  3. import NativeTryGamesWidget from "./NativeTryGamesWidget";
  4. import CompatibleTool from "./CompatibleTool";
  5. const { ccclass, property } = cc._decorator;
  6. @ccclass
  7. export default class NativeTryGameNode extends cc.Component {
  8. _data: any = null;
  9. _icon: cc.Sprite = null;
  10. _nameLabel: cc.Label = null;
  11. _gameJumpInterval: number = -1;
  12. _jumpInfo: any = null;
  13. _index: number = -1;
  14. _jumping: boolean = false; // 控制是否在跳转
  15. _isFirst: boolean = false;
  16. _mask: cc.Node = null;
  17. onLoad() {
  18. this._icon = cc.find("Mask/Icon", this.node).getComponent(cc.Sprite);
  19. this._mask = cc.find("Mask", this.node);
  20. }
  21. /**
  22. *
  23. * @param data 交叉推广数据
  24. * data:[{"date":{
  25. "jump_list": [
  26. { // 交叉推广挂件内容信息
  27. "icon": "http://xcx.youletd.com/img/icon/fgdxc.png",
  28. "name": "翻滚的香肠大冒险",
  29. "path": "",
  30. "js_jump": "true",
  31. "qr_code": "http://xcx.youletd.com/img/qrcode/q_fgdxc.jpg",
  32. "appid": "wx2c4ed4218224b042"
  33. }
  34. ]
  35. }
  36. "tryGameAd":tryGameAd
  37. }
  38. ]
  39. *
  40. */
  41. public init(data: any) {
  42. this._data = data;
  43. utils.showLog("原生抖动Node");
  44. if (this._data) {
  45. this._jumpInfo = this._data.jump_list;
  46. this._gameJumpInterval = this._data.jump_refresh_time;
  47. this._isFirst = true;
  48. this.node.active = true;
  49. if (PlatUtils.IsHuaWei) {
  50. this.node.getChildByName("source").getComponent(cc.Label).string = this._jumpInfo[this._index].date[0].source ? this._jumpInfo[this._index].date[0].source : "";
  51. }
  52. } else {
  53. this.node.active = false;
  54. }
  55. }
  56. /**
  57. * 跳转成功过后显示下一个
  58. */
  59. showNextItem() {
  60. this.unscheduleAllCallbacks();
  61. this.jump();
  62. this.schedule(this.jump, this._gameJumpInterval);
  63. }
  64. _nativeAd;
  65. onEnable() {
  66. this.jump();
  67. this.schedule(this.jump, this._gameJumpInterval);
  68. this.node.on(cc.Node.EventType.TOUCH_START, (event) => {
  69. if (this.node.opacity == 0) return;
  70. let tryGameAd = this._jumpInfo[this._index].tryGameAd;
  71. let adid = this._jumpInfo[this._index].date[0].adId;
  72. if (tryGameAd) {
  73. tryGameAd.reportAdClick({
  74. adId: adid
  75. });
  76. utils.showLog("抖动试玩上报" + "tryGameAd:" + JSON.stringify(tryGameAd) + "adid:" + adid)
  77. }
  78. utils.nativeNeedChange = true;
  79. utils.tryGameDate.splice(this._index, 1)
  80. if (utils._nativeTryGameNode) {
  81. utils._nativeTryGameNode.getComponent(NativeTryGamesWidget).init();
  82. }
  83. }, this);
  84. }
  85. onDisable() {
  86. this.unscheduleAllCallbacks();
  87. this.node.targetOff(this);
  88. }
  89. jump() {
  90. if (this._jumping) return;
  91. this._jumping = true;
  92. this._index = this._index + 1;
  93. if (this._index >= this._jumpInfo.length) {
  94. this._index = 0;
  95. }
  96. let contentSize = this._icon.node.getContentSize();
  97. if (this._jumpInfo[this._index]) {
  98. let remoteUrl = PlatUtils.IsOPPO ? this._jumpInfo[this._index].date[0].iconUrlList[0] : this._jumpInfo[this._index].date[0].icon;
  99. if (!remoteUrl || PlatUtils.IsHuaWei) {
  100. remoteUrl = this._jumpInfo[this._index].date[0].imgUrlList[0]
  101. }
  102. CompatibleTool.LoadRes(remoteUrl, (err, texture) => {
  103. if (!err && cc.isValid(this) && this._icon) {
  104. this._icon.spriteFrame = new cc.SpriteFrame(texture);
  105. this._icon.node.setContentSize(contentSize);
  106. if (this._isFirst) {
  107. utils.showLog("当前试玩第一次加载!!!!!");
  108. this._mask.active = true;
  109. this._isFirst = false;
  110. }
  111. }
  112. this._jumping = false;
  113. });
  114. let tryGameAd = this._jumpInfo[this._index].tryGameAd;
  115. let adid = this._jumpInfo[this._index].date[0].adId;
  116. if (tryGameAd && !tryGameAd.isReportShow) {
  117. tryGameAd.reportAdShow({
  118. adId: adid
  119. });
  120. tryGameAd.isReportShow = true;
  121. }
  122. } else {
  123. this._jumping = false;
  124. }
  125. this.node.opacity == 0 && (this.node.opacity = 255);
  126. }
  127. }