MoneyCat.js 8.3 KB

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