MoneyCat.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. const GameNotificationKey = require('../utils/GameEnum').GameNotificationKey;
  2. const GameModule = require('../utils/GameModule');
  3. const TapTapTool = require("../utils/TapTapTool");
  4. cc.Class({
  5. extends: cc.Component,
  6. properties: {
  7. moneyCatSkeleton: sp.Skeleton,
  8. moneyCatNode: cc.Node,
  9. clickAddMoney: cc.Prefab,
  10. clickAddMoneyCoin: cc.Prefab,
  11. // onceCoin: cc.Node,
  12. // twiceCoin: cc.Node,
  13. isHided: false,
  14. },
  15. // LIFE-CYCLE CALLBACKS:
  16. onLoad () {
  17. this.isPlaying = false;
  18. this._isTouch = false;
  19. this.showNewCoin = true;
  20. this.node.on('touchend', () => {
  21. this.clickCat(false);
  22. }, this);
  23. this.addMoneyPool = new cc.NodePool();
  24. this.addMoneyCoinPool = new cc.NodePool();
  25. this.addMoneyAutoCoinPool = new cc.NodePool();
  26. GameEvent.on(GameNotificationKey.AutoClickGold, this, this.autoClickCat);
  27. GameEvent.on(GameNotificationKey.ClickAddMoney, this, this.clickCat);
  28. GameModule.homeGuide.on('Fire_state5', this.stopCat, this);
  29. GameModule.homeGuide.on('Fire_state6', this.clickCat, this);
  30. if (GameModule.homeGuide.getComponent('HomeGuide').isPassGuideState('state5')) {
  31. if (!GameModule.homeGuide.getComponent('HomeGuide').isPassGuideState('state6')) {
  32. GameModule.homeGuide.getComponent('HomeGuide').handleState('state6');
  33. this.stopCat();
  34. }
  35. }
  36. GameEvent.on('just_cat_hit', this, this.catHit);
  37. if (Global.isLongPressClick) {
  38. this.addLongTap();
  39. }
  40. },
  41. stopCat() {
  42. this.moneyCatSkeleton.setAnimation(0, 'stand', false);
  43. this.moneyCatSkeleton.timeScale = 0;
  44. },
  45. addLongTap() {
  46. this.node.on(cc.Node.EventType.TOUCH_START, (event) => {
  47. //// 如果有购买商品 一秒钟点击十次
  48. // console.log('开始点击');
  49. if (!this._isTouch) {
  50. if (Global.isLongPressClick === true) {
  51. /// 最起码要按住1秒
  52. this._addClick = false;
  53. this._timeCount = 0;
  54. this.schedule(this.timeAction, 1);
  55. this._isTouch = true;
  56. }
  57. }
  58. });
  59. this.node.on(cc.Node.EventType.TOUCH_END, (event) => {
  60. // console.log("点击结束");
  61. if (this._isTouch) {
  62. if (Global.isLongPressClick === true && this._addClick) {
  63. let clickCount = 1 / GameModule.userInfo.secondClick;
  64. clickCount -= 10;
  65. GameModule.userInfo.secondClick = 1 / clickCount;
  66. this._addClick = false;
  67. } else {
  68. this._addClick = true;
  69. }
  70. this._isTouch = false;
  71. this.unschedule(this.timeAction, this);
  72. }
  73. }, this);
  74. this.node.on(cc.Node.EventType.TOUCH_MOVE, (event) => {
  75. let x = event.getLocationX();
  76. let y = event.getLocationY();
  77. let startLocation = event.getStartLocation();
  78. // console.log('移动');
  79. if (this._isTouch && ( Math.abs(x - startLocation.x) > 10 || Math.abs(y - startLocation.y) > 10)) {
  80. if (Global.isLongPressClick === true && this._addClick) {
  81. let clickCount = 1 / GameModule.userInfo.secondClick;
  82. clickCount -= 10;
  83. GameModule.userInfo.secondClick = 1 / clickCount;
  84. this._addClick = false;
  85. } else {
  86. this._addClick = true;
  87. }
  88. this.unschedule(this.timeAction, this);
  89. this._isTouch = false;
  90. }
  91. }, this);
  92. },
  93. timeAction() {
  94. this._timeCount += 1;
  95. if (this._timeCount == 1) {
  96. if (!this._addClick) {
  97. /// 一秒钟点击的次数
  98. let clickCount = 0;
  99. if (GameModule.userInfo.secondClick == 0) {
  100. clickCount = 10;
  101. } else {
  102. clickCount = 1 / GameModule.userInfo.secondClick;
  103. clickCount += 10;
  104. }
  105. GameModule.userInfo.secondClick = 1 / clickCount;
  106. this._addClick = true;
  107. }
  108. /// 否则每秒钟添加 10次点击
  109. } else {
  110. GameModule.userInfo.clickCount += 10;
  111. }
  112. },
  113. clickCat(isAuto = false) {
  114. if (!isAuto && this.isHided) {
  115. GameEvent.fire(GameNotificationKey.TabbarClickCat);
  116. return;
  117. }
  118. //自动点击时候并且收起招财猫将不播放动画
  119. if (isAuto && this.isHided) {
  120. GameModule.userInfo.gold = TapTapTool.add(GameModule.userInfo.gold, GameModule.userInfo.coinTap);
  121. return;
  122. }
  123. if (isAuto) {
  124. //自动点击金币出现动画
  125. if (this.showNewCoin) {
  126. let coinItem = null;
  127. if (this.addMoneyAutoCoinPool.size() > 0) {
  128. coinItem = this.addMoneyAutoCoinPool.get();
  129. } else {
  130. coinItem = cc.instantiate(this.clickAddMoneyCoin);
  131. }
  132. this.node.addChild(coinItem);
  133. coinItem.x = 0;
  134. coinItem.y = 190;
  135. coinItem.active = true;
  136. this.showNewCoin = false;
  137. let coinManager = coinItem.getComponent('ClickAddMoneyCoin');
  138. coinManager.showAutoCoin( () => {
  139. this.showNewCoin = true;
  140. }, () => {
  141. this.addMoneyAutoCoinPool.put(coinItem);
  142. });
  143. }
  144. } else {
  145. GameModule.audioMng.playClickCat();
  146. let coinItem = null;
  147. if (this.addMoneyCoinPool.size() > 0) {
  148. coinItem = this.addMoneyCoinPool.get();
  149. } else {
  150. coinItem = cc.instantiate(this.clickAddMoneyCoin);
  151. }
  152. this.node.addChild(coinItem);
  153. coinItem.x = 0;
  154. coinItem.y = 190;
  155. coinItem.active = true;
  156. let coinManager = coinItem.getComponent('ClickAddMoneyCoin');
  157. coinManager.showCoin( () => {
  158. this.addMoneyCoinPool.put(coinItem);
  159. });
  160. }
  161. let item = null;
  162. if (this.addMoneyPool.size() > 0) {
  163. item = this.addMoneyPool.get();
  164. } else {
  165. item = cc.instantiate(this.clickAddMoney);
  166. }
  167. this.node.addChild(item);
  168. item.x = 0;
  169. item.y = 230;
  170. item.active = true;
  171. let itemManager = item.getComponent('ClickAddMoney');
  172. itemManager.showAddMoney( () => {
  173. this.addMoneyPool.put(item);
  174. }, isAuto);
  175. //招财猫的动画
  176. if (this.isPlaying) { return }
  177. this.isPlaying = true;
  178. this.moneyCatSkeleton.timeScale = 1;
  179. this.moneyCatSkeleton.setAnimation(0, 'hit', false);
  180. this.moneyCatSkeleton.setCompleteListener(() => {
  181. this.isPlaying = false;
  182. this.moneyCatSkeleton.setAnimation(0, 'stand', true);
  183. });
  184. },
  185. catHit() {
  186. //招财猫的动画
  187. this.isPlaying = true;
  188. this.moneyCatSkeleton.timeScale = 1;
  189. this.moneyCatSkeleton.setAnimation(0, 'hit', false);
  190. this.moneyCatSkeleton.setCompleteListener(() => {
  191. this.isPlaying = false;
  192. this.moneyCatSkeleton.setAnimation(0, 'stand', true);
  193. });
  194. },
  195. autoClickCat() {
  196. this.clickCat(true);
  197. },
  198. start () {
  199. },
  200. // update (dt) {
  201. // if (this._isTouch && this._addClick) {
  202. // }
  203. // },
  204. });