goFishingScene.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /**
  2. * 绘制钓鱼线,线在水里飘的效果
  3. */
  4. cc.Class({
  5. extends: cc.Component,
  6. properties: {
  7. },
  8. // LIFE-CYCLE CALLBACKS:
  9. onLoad () {
  10. Module.goFishingObj = this;
  11. this.touchEnbled = true;
  12. this.touchId = [];
  13. Module.state = true;
  14. this.moveCount = 0;
  15. this.isShouGou = true
  16. },
  17. start () {
  18. this.init();
  19. },
  20. /**
  21. * 初始化鱼钩
  22. */
  23. init: function() {
  24. this.windowSize=cc.view.getVisibleSize();
  25. this.y1 = this.windowSize.height + 300;
  26. this.x1 = this.windowSize.width / 2;
  27. this.y2 = this.windowSize.height - this.windowSize.height/ 2 / 2 ;
  28. this.x2 = this.windowSize.width / 2;
  29. this.y3 = this.windowSize.height / 2 -50;
  30. this.x3 = this.windowSize.width / 2;
  31. this.hook = this.node.getChildByName('hook');
  32. this.hook.x = this.x3 - 14;
  33. this.hook.y = this.y3 + 3;
  34. this.limits = this.node.getChildByName('limits');
  35. this.limits.x = this.x3 - 14;
  36. this.limits.y = this.y3 + 3;
  37. var ctx = this.getComponent(cc.Graphics);
  38. if (!ctx) ctx = this.addComponent(cc.Graphics);
  39. ctx.clear();
  40. ctx.lineWidth = 2;
  41. ctx.strokeColor = cc.Color.BLACK;
  42. ctx.moveTo(this.x1, this.y1);
  43. ctx.bezierCurveTo(this.x1, this.y1, this.x2, this.y2, this.x3, this.y3);
  44. ctx.stroke();
  45. Module.fishingLine = this.fishingLine = ctx;
  46. this.node.on(cc.Node.EventType.TOUCH_START, this.touchBegin.bind(this), this );
  47. this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMoving.bind(this), this );
  48. this.node.on(cc.Node.EventType.TOUCH_END, this.touchEnd.bind(this), this );
  49. this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchCancel.bind(this), this );
  50. },
  51. /**
  52. * 触摸开始
  53. */
  54. touchBegin:function(touch,event){
  55. if (this.touchId.length != 0) return;
  56. this.touchId.push(touch.getID());
  57. this.lastTouch = touch.getLocation();
  58. },
  59. /**
  60. * 移动中
  61. */
  62. touchMoving:function(touch,event){
  63. if (Module.isShouGou) return;
  64. var width = this.windowSize.width;
  65. if (touch.getID() != this.touchId[0]) return;
  66. if (touch.getLocation() == undefined || this.lastTouch == undefined) return;
  67. this.isShouGou = true;
  68. var p = touch.getLocation();
  69. var movex = p.x - this.lastTouch.x;
  70. var movey = p.y - this.lastTouch.y;
  71. if(this.x3 >= 0 && this.x3 <= width){
  72. var len = (p.x-this.lastTouch.x)*(p.x-this.lastTouch.x)+(p.y-this.lastTouch.y)*(p.y-this.lastTouch.y)
  73. var length = Math.pow(len,2);
  74. if(length > 1){
  75. this.x3+= movex * 1.2;
  76. this.lastTouch = p;
  77. }
  78. }
  79. if(this.x3 < 0){ this.x3 = 0 }
  80. if(this.x3 > this.windowSize.width ){ this.x3 = this.windowSize.width }
  81. if (this.fishingLine) {
  82. this.fishingLine.clear();
  83. this.fishingLine.moveTo(this.x1, this.y1);
  84. this.fishingLine.bezierCurveTo(this.x1, this.y1, this.x2, this.y2, this.x3, this.y3);
  85. this.fishingLine.stroke();
  86. Module.fishingLine = this.fishingLine;
  87. Module.state = true;
  88. this.moveCount = 0;
  89. this.hook.x = this.x3- 14;
  90. this.limits.x = this.hook.x;
  91. this.setHookRotation();
  92. }
  93. },
  94. /**
  95. * 设置鱼钩角度
  96. */
  97. setHookRotation: function() {
  98. var r = 0;
  99. if (this.hook.x > this.windowSize.width / 2) {
  100. r = -((this.hook.x - this.windowSize.width / 2)/10 / 2);
  101. } else {
  102. r = Math.abs((this.hook.x- 320))/10 /2;
  103. }
  104. this.limits.x = this.hook.x;
  105. this.hook.rotation = r;
  106. },
  107. /**
  108. * 触摸结束
  109. */
  110. touchEnd:function(touch, event){
  111. if (touch.getID() == this.touchId[0]) {
  112. this.touchId = [];
  113. }
  114. },
  115. /**
  116. * 触摸取消
  117. */
  118. touchCancel: function(touch, event){
  119. this.touchId = [];
  120. if (!Module.state) return;
  121. },
  122. /**
  123. * 钓鱼线摆动方向
  124. */
  125. update (dt) {
  126. //收钩
  127. if (Module.isShouGou && this.isShouGou) {
  128. if (this.x2 > this.windowSize.width / 2) {
  129. this.x2 -= 2;
  130. } else {
  131. this.x2 += 2;
  132. }
  133. var flag = 0;
  134. if (this.x3 > this.windowSize.width / 2) {
  135. this.x3 -= 2;
  136. flag = 1;
  137. } else {
  138. this.x3 += 2;
  139. flag = 2;
  140. }
  141. // if () {
  142. // }
  143. if (this.x3 > this.windowSize.width / 2 && flag == 2) {
  144. this.isShouGou = false;
  145. this.hook.rotation = 0;
  146. } else if (this.x3 < this.windowSize.width / 2 && flag == 1){
  147. this.isShouGou = false;
  148. this.hook.rotation = 0;
  149. }
  150. if (this.fishingLine) {
  151. this.fishingLine.clear();
  152. this.fishingLine.moveTo(this.x1, this.y1);
  153. this.fishingLine.bezierCurveTo(this.x1, this.y1, this.x2, this.y2, this.x3, this.y3);
  154. this.fishingLine.stroke();
  155. Module.fishingLine = this.fishingLine;
  156. this.hook.x = this.x3- 14;
  157. this.limits.x = this.hook.x;
  158. this.setHookRotation();
  159. }
  160. return;
  161. }
  162. if (!Module.state || this.moveCount == 3) return;
  163. var kAB = (this.y2-this.y1)/((this.x2) -this.x1);
  164. var kBC = (this.y3-this.y2)/(this.x3- (this.x2));
  165. if (kAB == Infinity) kAB = 0;
  166. if (kBC == Infinity) kBC = 0;
  167. //贝塞尔曲线是否成直线
  168. if (parseInt(kAB) == parseInt(kBC)) {
  169. Module.state = false;
  170. return;
  171. }
  172. if (this.x3 == this.windowSize.width / 2) {
  173. this.x2 = this.windowSize.width / 2;
  174. Module.state = false;
  175. return;
  176. } else if (this.x2 < this.x3) {
  177. if (this.moveCount != 1 && this.moveCount < 3) ++this.moveCount;
  178. this.x2 += 8;
  179. } else {
  180. if (this.moveCount != 2 && this.moveCount < 3) this.moveCount+=2;
  181. this.x2 -= 8;
  182. }
  183. //绘制钓鱼线
  184. if (this.fishingLine) {
  185. this.fishingLine.clear();
  186. this.fishingLine.moveTo(this.x1, this.y1);
  187. this.fishingLine.bezierCurveTo(this.x1, this.y1, this.x2, this.y2, this.x3, this.y3);
  188. this.fishingLine.stroke();
  189. Module.fishingLine = this.fishingLine;
  190. this.hook.x = this.x3- 14;
  191. this.limits.x = this.hook.x;
  192. this.setHookRotation();
  193. }
  194. },
  195. //还原鱼钩状态
  196. restore: function() {
  197. this.y1 = this.windowSize.height + 300;
  198. this.x1 = this.windowSize.width / 2;
  199. this.y2 = this.windowSize.height - this.windowSize.height/ 2 / 2 ;
  200. this.x2 = this.windowSize.width / 2;
  201. this.y3 = this.windowSize.height / 2 -50;
  202. this.x3 = this.windowSize.width / 2;
  203. this.hook.x = this.x3 - 14;
  204. this.hook.y = this.y3 + 3;
  205. var ctx = this.getComponent(cc.Graphics);
  206. if (!ctx) ctx = this.addComponent(cc.Graphics);
  207. ctx.clear();
  208. ctx.lineWidth = 2;
  209. ctx.strokeColor = cc.Color.BLACK;
  210. ctx.moveTo(this.x1, this.y1);
  211. ctx.bezierCurveTo(this.x1, this.y1, this.x2, this.y2, this.x3, this.y3);
  212. ctx.stroke();
  213. Module.fishingLine = this.fishingLine = ctx;
  214. this.hook.rotation = 0;
  215. this.limits.x = this.hook.x;
  216. },
  217. });