JumpIcon.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { LabelComponent, Node, SpriteComponent, SpriteFrame } from "cc";
  2. // Learn TypeScript:
  3. // - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
  4. // Learn Attribute:
  5. // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
  6. // Learn life-cycle callbacks:
  7. // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
  8. import { LiangLiangSDK } from "./liangliangSDK";
  9. const {ccclass, property} = cc._decorator;
  10. interface JumpData
  11. {
  12. icon:string,
  13. app_id : string,
  14. game_id : string,
  15. big_icon: string,
  16. app_path: string,
  17. skipIcon: boolean;
  18. name:string;
  19. }
  20. var wx = window["wx"];
  21. @ccclass
  22. export default class JumpIcon extends cc.Component
  23. {
  24. static _allspriteFrame :{[key:string]:SpriteFrame} = {};
  25. @property({
  26. type:SpriteComponent
  27. })
  28. sprite:SpriteComponent=null;
  29. @property({
  30. type:LabelComponent
  31. })
  32. nameLabel:LabelComponent = null;
  33. _data: JumpData;
  34. Init(data:JumpData)
  35. {
  36. this._data = data;
  37. if(!this._data.skipIcon)
  38. {
  39. var iconname = data.icon;
  40. if(this.sprite.node.width > 196 )
  41. {
  42. iconname = data.big_icon;
  43. }
  44. if(JumpIcon._allspriteFrame[iconname])
  45. {
  46. this.sprite.spriteFrame = JumpIcon._allspriteFrame[iconname];
  47. }
  48. else
  49. {
  50. let self = this;
  51. cc.loader.load(iconname, (err, texture) =>
  52. {
  53. if (texture && !err)
  54. {
  55. var spriteFrame = new cc.SpriteFrame(texture);
  56. JumpIcon._allspriteFrame[iconname] = spriteFrame;
  57. self.sprite.spriteFrame = spriteFrame;
  58. }
  59. });
  60. }
  61. }
  62. this.nameLabel.string = this._data.name;
  63. }
  64. start ()
  65. {
  66. }
  67. update (dt)
  68. {
  69. }
  70. onEnable()
  71. {
  72. this.node.on(Node.EventType.TOUCH_START, this.onTouchStartCallback, this, true);
  73. }
  74. onDisable()
  75. {
  76. this.node.off(Node.EventType.TOUCH_START, this.onTouchStartCallback, this, true);
  77. }
  78. onTouchStartCallback()
  79. {
  80. let self = this;
  81. if(window[wx]!=null)
  82. {
  83. wx.navigateToMiniProgram({
  84. appId: this._data.app_id,
  85. path: this._data.app_path,
  86. success: (res) =>
  87. {
  88. LiangLiangSDK.CpaReport(self._data);
  89. },
  90. fail: (res) => {},
  91. complete: (res) => {},
  92. });
  93. }
  94. else
  95. {
  96. console.log("点中了图标 : " + this._data.name);
  97. }
  98. }
  99. }