LevelHomeCoin.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. const DWTool = require('../utils/DWTool');
  2. cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. coinNode: cc.Node,
  6. totalRateLabel: cc.Label,
  7. coinCount: 0,
  8. totalRate: {
  9. get: function() {
  10. if (!this._totalRate) {
  11. this._totalRate = 0;
  12. }
  13. return this._totalRate;
  14. },
  15. set: function(value) {
  16. this._totalRate = value;
  17. this.totalRateLabel.string = DWTool.coinParse(this._totalRate);
  18. }
  19. },
  20. coinSpine: sp.Skeleton,
  21. isPlay: false,
  22. isPlaying: false,
  23. index: 0,
  24. },
  25. // LIFE-CYCLE CALLBACKS:
  26. onLoad () {
  27. this.initY = parseFloat(this.node.y)
  28. },
  29. getOwner() {
  30. return this.node;
  31. },
  32. /**
  33. * 初始化静态金币Spine动画
  34. * @param {Number} coinIndex 金币下标,默认[0,1,2]
  35. */
  36. initStatic(coinIndex) {
  37. let animKeyArray = ['jinbi_1', 'jinbi_2', 'jinbi_3'];
  38. let animKey;
  39. if(coinIndex <= animKeyArray.length - 1) {
  40. animKey = animKeyArray[coinIndex]
  41. } else {
  42. animKey = coinIndex % animKeyArray.length;
  43. }
  44. this.coinSpine.setAnimation(0, animKey, true)
  45. },
  46. /**
  47. * 初始化飞行金币Spine动画
  48. */
  49. initAnim() {
  50. let animKeyArray = ['jinbi_zhuangdong1', 'jinbi_zhuangdong2', 'jinbi_zhuangdong3']
  51. //随机设置转动动画
  52. let ran = Math.floor(Math.random() * 3);
  53. let animKey = animKeyArray[ran]
  54. this.totalRateLabel.node.active = false;
  55. this.coinSpine.setAnimation(0, animKey, true)
  56. },
  57. showAnimation() {
  58. if (this.isPlaying) { return; }
  59. this.node.stopAllActions();
  60. this.fixPosition();
  61. this.node.active = true;
  62. this.isPlayed = true;
  63. this.isPlaying = true;
  64. let jumpHeight = 175;
  65. let duration = 0.3;
  66. let animationArray = [];
  67. let moveAction1 = cc.moveBy(duration, 0, jumpHeight).easing(cc.easeCubicActionOut());
  68. let moveAction2 = cc.moveBy(duration, 0, -95).easing(cc.easeCubicActionIn());
  69. animationArray.push(moveAction1);
  70. animationArray.push(moveAction2);
  71. while(jumpHeight > 0.1) {
  72. jumpHeight = jumpHeight - (jumpHeight / 3);
  73. duration = duration - (duration / 3);
  74. let upAction = cc.moveBy(duration, 0, jumpHeight).easing(cc.easeCubicActionOut());
  75. let downAction = cc.moveBy(duration, 0, -jumpHeight).easing(cc.easeCubicActionIn());
  76. animationArray.push(upAction);
  77. animationArray.push(downAction);
  78. }
  79. let callback = cc.callFunc(() => {
  80. this.isPlaying = false;
  81. });
  82. animationArray.push(callback);
  83. this.node.runAction(cc.sequence(animationArray));
  84. },
  85. updateAnimation() {
  86. if (this.isPlaying) { return; }
  87. this.node.active = true
  88. this.node.y = -28;
  89. this.node.stopAllActions();
  90. this.isPlaying = true;
  91. let upAction = cc.moveBy(0.1, 0, 30);
  92. let downAction = cc.moveBy(0.1, 0, -30);
  93. let callback = cc.callFunc(() => {
  94. this.isPlaying = false;
  95. });
  96. this.coinNode.runAction(cc.sequence(upAction, downAction, callback));
  97. },
  98. // 修复金币高度问题:
  99. // 微信环境下金币飞出的同时触发手机的锁屏,
  100. // 或者最小化微信可能会导致金币位置错误
  101. fixPosition() {
  102. if(this.node.y != this.initY) {
  103. this.node.y = this.initY;
  104. }
  105. },
  106. start () {
  107. },
  108. // update (dt) {},
  109. });