dishu.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. touchFlag: false,
  5. _isEnemy: false,
  6. },
  7. init(parents, randomIndex) {
  8. this.父级 = parents;
  9. this._isEnemy = false;
  10. if (this.父级.name == "敌方父级") {
  11. this._isEnemy = true;
  12. }
  13. this.添加事件();
  14. this._name = randomIndex;
  15. if (randomIndex == 0)
  16. this._name = "";
  17. this.PlayDragonBones(this.animationDisplay, "jian" + this._name, this.默认状态, 1);
  18. this.surplusCount = this.击打次数(randomIndex);
  19. this.clickCount = 0;
  20. this.reduceBloodCount = 1;
  21. this.目标父级 = this.父级;
  22. this.node.position = new cc.Vec2(0, -190);
  23. this.canClick = true;
  24. this.伸头();
  25. },
  26. 添加事件() {
  27. this.animationDisplay = this.getComponent(dragonBones.ArmatureDisplay);
  28. if (this._isEnemy) {
  29. } else {
  30. this.node.on(cc.Node.EventType.TOUCH_START, function () {
  31. if (!this.canClick)
  32. return;
  33. ObjectPools.playSimpleAudioEngine(0);
  34. var pos = this.node.parent.parent.position;
  35. Games.拳套.position = pos;
  36. this.PlayDragonBones(Games.glovesDragon, "attack2", this.隐藏拳套, 1);
  37. this.击中();
  38. }, this);
  39. Games.node.on(cc.Node.EventType.TOUCH_MOVE, function (event) {
  40. if (!this.canClick)
  41. return;
  42. if (Games.checkTouch(this.node, event.getLocation())) {
  43. if (this.touchFlag == false) {
  44. this.lastPos = event.getLocation();
  45. this.touchFlag = true;
  46. }
  47. } else {
  48. if (this.touchFlag == true) {
  49. this.currentPos = event.getLocation();
  50. this.方向判断();
  51. }
  52. }
  53. }, this);
  54. ObjectPools.playSimpleAudioEngine(3);
  55. }
  56. },
  57. 方向判断() {
  58. if (!this.canClick)
  59. return;
  60. var valuex = this.currentPos.x - this.lastPos.x;
  61. if (Math.abs(valuex) < 5)
  62. return;
  63. this.touchFlag = false;
  64. // 左右滑动
  65. if (valuex > 0) {
  66. ObjectPools.playSimpleAudioEngine(0);
  67. var pos = this.node.parent.parent.position;
  68. Games.拳套.position = pos;
  69. Games.拳套.scale = new cc.Vec2(1, 1);
  70. this.PlayDragonBones(Games.glovesDragon, "attack1", this.隐藏拳套, 1);
  71. this.右击();
  72. }
  73. else {
  74. ObjectPools.playSimpleAudioEngine(0);
  75. var pos = this.node.parent.parent.position;
  76. Games.拳套.position = pos;
  77. Games.拳套.scale = new cc.Vec2(-1, 1);
  78. this.PlayDragonBones(Games.glovesDragon, "attack1", this.隐藏拳套, 1);
  79. this.左击();
  80. }
  81. },
  82. 默认状态(event) {
  83. var scripts = this.node.getComponent("dishu");
  84. scripts.PlayDragonBones(scripts.animationDisplay, "idle" + scripts._name, scripts.默认状态, 1);
  85. },
  86. // 伸头后倒计时 2秒后自动缩头
  87. 伸头() {
  88. var self = this;
  89. this.scheduleOnce(function () {
  90. if (this.canClick) {
  91. console.log("ceshi");
  92. this.canClick = false;
  93. this.unscheduleAllCallbacks();
  94. if (!this._isEnemy) {
  95. ObjectPools.playSimpleAudioEngine(5);
  96. }
  97. var finished = cc.callFunc(function () {
  98. self.node.stopAllActions();
  99. ObjectPools.地鼠回收(self.node);
  100. Games.随机生成(self.父级);
  101. });
  102. this.缩头动作 = cc.sequence(cc.moveBy(0.2, cc.p(0, -150)), finished);
  103. this.node.runAction(this.缩头动作);
  104. }
  105. }, 2);
  106. },
  107. 击中() {
  108. this.PlayDragonBones(this.animationDisplay, "yun" + this._name, this.默认状态, 1);
  109. this.连击();
  110. },
  111. 左击() {
  112. var names;
  113. if (this._name == "") {
  114. names = "11";
  115. } else {
  116. names = 10 + parseInt(this._name) + 1;
  117. }
  118. //console.log("左击" + "attack" + names);
  119. this.PlayDragonBones(this.animationDisplay, "attack" + names, this.默认状态, 1, -1);
  120. this.连击();
  121. },
  122. 右击() {
  123. var names;
  124. if (this._name == "") {
  125. names = "11";
  126. } else {
  127. names = 10 + parseInt(this._name) + 1;
  128. }
  129. // console.log("左击" + "attack" + names);
  130. this.PlayDragonBones(this.animationDisplay, "attack" + names, this.默认状态, 1);
  131. this.连击();
  132. },
  133. 连击() {
  134. this.surplusCount--;
  135. this.clickCount++;
  136. if (this.node.parent != null) {
  137. if (this.clickCount >= 3) {
  138. ObjectPools.playSimpleAudioEngine(4);
  139. Games.连击播放(this.node.parent.parent.position, Games.连击动画, this.clickCount);
  140. }
  141. }
  142. if (this.surplusCount == 0) {
  143. this.挂掉();
  144. }
  145. },
  146. 隐藏拳套(event) {
  147. Games.拳套.position = cc.Vec2.ZERO;
  148. },
  149. 挂掉() {
  150. if (!this.canClick)
  151. return;
  152. ObjectPools.playSimpleAudioEngine(2);
  153. this.canClick = false;
  154. this.node.stopAllActions();
  155. this.unscheduleAllCallbacks();
  156. var randomIndex = parseInt(Math.random() * 2, 10);
  157. if (randomIndex == 0) {
  158. var names;
  159. if (this._name == "") {
  160. names = "21";
  161. } else {
  162. names = 20 + parseInt(this._name) + 1;
  163. }
  164. this.PlayDragonBones(this.animationDisplay, "attack" + names, this.回收, 1);
  165. } else {
  166. this.PlayDragonBones(this.animationDisplay, "cry" + this._name, this.回收, 1);
  167. }
  168. },
  169. 回收(event) {
  170. var scripts = this.node.getComponent("dishu");
  171. Games.掉血(scripts.目标父级, scripts.reduceBloodCount);
  172. ObjectPools.地鼠回收(this.node);
  173. Games.随机生成(scripts.父级);
  174. },
  175. PlayDragonBones(animationDisplays, newAnimation, completeCallback, playTimes, scaleX = 1) { //动态加载龙骨
  176. animationDisplays.playAnimation(newAnimation, playTimes);
  177. this.node.scale = new cc.Vec2(scaleX, 1);
  178. animationDisplays.addEventListener(dragonBones.EventObject.COMPLETE, completeCallback, animationDisplays);//.apply(this));
  179. },
  180. 击打次数(index) {
  181. var count;
  182. // idle:普通 idle1:安全帽 idel2:海盗帽 idle:南瓜帽
  183. switch (index + 1) {
  184. case 1: count = 1; break;
  185. case 2: count = 6; break;
  186. case 3: count = 3; break;
  187. case 4: count = 9; break;
  188. }
  189. return count;
  190. },
  191. });