Turnable.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import BtnClosePanel from "../../component/BtnClosePanel";
  2. import { AdFun } from "../../data/AdData";
  3. import TurnableItem from "./TurnableItem";
  4. const { ccclass, property } = cc._decorator;
  5. /**
  6. * 转盘
  7. * @author 邹勇 kaka
  8. */
  9. @ccclass
  10. export default class Turnable extends cc.Component {
  11. @property(cc.Node)
  12. node_turn: cc.Node = null;
  13. @property(cc.Node)
  14. node_items: cc.Node[] = [];
  15. @property(cc.Node)
  16. btn_draw: cc.Node = null;
  17. @property(cc.Sprite)
  18. btn_close: cc.Sprite = null;
  19. @property(cc.RichText)
  20. lbl_left: cc.RichText = null;
  21. private audioId: number;
  22. private endAngle = 0
  23. private startDraw = false
  24. private slowDis = 720
  25. private slowAngle = 0
  26. private perAdd = 10
  27. private perMinus = 0
  28. onLoad() {
  29. this.initData();
  30. }
  31. /**
  32. * 初始化奖励和抽奖次数
  33. */
  34. private initData() {
  35. if (gData.turnable.config) {
  36. for (let i = 0; i < gData.turnable.config.length; i++) {
  37. let sc = this.node_items[i].getComponent(TurnableItem);
  38. sc.initData(gData.turnable.config[i].type);
  39. }
  40. }
  41. gData.turnable.requestData();
  42. this.updateLeftTimes();
  43. }
  44. /** 点击抽奖 */
  45. clickDraw() {
  46. if (gData.turnable.leftTimes > 0) {
  47. mk.ad.watchAd((success: boolean) => {
  48. mk.console.log("watchAD:" + success);
  49. if (success) {
  50. gData.adData.watchVideo(AdFun.turntable);
  51. }
  52. });
  53. }
  54. // this.play(2);
  55. }
  56. private async play(index) {
  57. this.audioId = await mk.audio.playEffect("turnableplay", true);
  58. this.btn_close.getComponent(BtnClosePanel).block_click = true;
  59. this.perAdd = 10
  60. this.node_turn.angle += 360 * 5
  61. this.endAngle = index * -60
  62. this.slowAngle = this.endAngle + this.slowDis
  63. this.perMinus = this.perAdd / (this.slowDis / (this.perAdd * 0.5))
  64. this.startDraw = true
  65. // this.node_turn.angle = 1800;
  66. // let tar = index * 60;
  67. // cc.tween(this.node_turn)
  68. // .to(3, { angle: tar })
  69. // .call(this.playOver, this).start();
  70. }
  71. private playOver() {
  72. mk.audio.stopEffect(this.audioId);
  73. this.btn_close.getComponent(BtnClosePanel).block_click = false;
  74. this.updateLeftTimes();
  75. mk.ui.openPanel("module/reward/reward");
  76. }
  77. updateLeftTimes() {
  78. this.lbl_left.string = `剩余:<color=#FFED99>${gData.turnable.leftTimes}</color>次`;
  79. }
  80. update() {
  81. if (gData.turnable.requestFinish) {
  82. this.updateLeftTimes();
  83. gData.turnable.requestFinish = false;
  84. }
  85. if (gData.turnable.adData) {
  86. this.play(2);
  87. gData.reward.data = gData.turnable.adData;
  88. gData.turnable.adData = null;
  89. }
  90. if (this.startDraw) {
  91. this.node_turn.angle -= this.perAdd
  92. //开始减速
  93. if (this.node_turn.angle <= this.slowAngle) {
  94. if (this.perAdd <= this.perMinus * 4) {
  95. this.perAdd -= this.perMinus * 0.01
  96. }
  97. else {
  98. this.perAdd -= this.perMinus
  99. }
  100. }
  101. if (this.node_turn.angle <= this.endAngle) {
  102. this.startDraw = false
  103. }
  104. }
  105. }
  106. onDestroy() {
  107. }
  108. }