Collect.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. },
  5. start () {
  6. this._init();
  7. this.diamondCount1 = 1;
  8. this.diamondCount2 = 3;
  9. this.diamondCount3 = 5;
  10. this.diamondCount4 = 4;
  11. this.diamondCount = this.diamondCount3;
  12. this.tests = true;
  13. // GlobalD
  14. },
  15. playScale : function(){
  16. this.diamond.y = this._y;
  17. this.diamond.opacity = 255;
  18. this.diamond.stopAllActions();
  19. var s = cc.sequence(cc.scaleTo(0.5, 2.5),cc.scaleTo(0.5, 2));
  20. var repeat = cc.repeatForever(s);
  21. this.diamond.runAction(repeat);
  22. var s1 = cc.sequence(cc.rotateTo(0.1, -5),cc.rotateTo(0.1, 5));
  23. var r = cc.repeatForever(s1);
  24. this.diamond.runAction(r);
  25. this.onClickTag = true;
  26. },
  27. _addPrefab : function(callback){
  28. cc.loader.loadRes("prefab/diamond", function (err, texture) {
  29. this.diamond = cc.instantiate(texture);
  30. this.diamond.name = "diamond";
  31. var tempNode = this.node;
  32. if (tempNode.getChildByName(this.diamond.name)!=null) {
  33. tempNode.getChildByName(this.diamond.name).destroy();
  34. }
  35. tempNode.addChild(this.diamond);
  36. this.diamond.x = this._x;
  37. this.diamond.y = this._y;
  38. this.diamond.active = false;
  39. if (callback != null) {
  40. callback();
  41. }
  42. }.bind(this));
  43. },
  44. _init: function () {
  45. this.GoldCoin = cc.find("Canvas/UICamera/GoldCoin");
  46. this.diamondIcon = cc.find("Canvas/UICamera/GoldCoin/DiamondIcon");
  47. this.canvas = cc.find("Canvas");
  48. //0 未播放 1正播放
  49. this.state = 0;
  50. this.TipName = '';
  51. this._x = 0;
  52. this._y = this.node.height/2;
  53. this.onClickTag = false;
  54. this.count = 0;
  55. this._addPrefab(function () {
  56. this.diamond.active = false;
  57. this.diamond.x = this._x;
  58. this.diamond.y = this._y;
  59. this.diamond.opacity = 0;
  60. this.diamond.on(cc.Node.EventType.TOUCH_END, function (event) {
  61. // cc.log("点了钻石",event.getLocation());
  62. let touchPos = event.getLocation();
  63. this.harvest(touchPos);
  64. }.bind(this));
  65. }.bind(this));
  66. },
  67. //判断是否可以
  68. getOnClickTag : function(){
  69. // console.log("点击钻石 之后",this.onClickTag);
  70. return this.onClickTag;
  71. },
  72. addCount : function () {
  73. this.diamond.active = true;
  74. this.count++;
  75. if (this.count == this.diamondCount) {
  76. this.playScale();
  77. }else if(this.count == this.diamondCount1) {
  78. this.diamond.opacity = 50;
  79. this.diamond.scale = 1;
  80. this.upDown();
  81. }else if(this.count == this.diamondCount2) {
  82. this.diamond.opacity = 100;
  83. this.diamond.scale = 1.25;
  84. }else if(this.count == this.diamondCount3) {
  85. this.diamond.opacity = 150;
  86. this.diamond.scale = 2;
  87. }else if (this.count >= 6) {
  88. // this.harvest();
  89. }
  90. },
  91. upDown : function(){
  92. var s = cc.sequence(cc.moveBy(0.5, 0,25),cc.moveBy(0.5, 0,-25));
  93. var f = cc.repeatForever(s);
  94. this.diamond.runAction(f)
  95. },
  96. harvest : function (touchPos) {
  97. if (this.count < this.diamondCount1) {
  98. return;
  99. }
  100. var tempCount = this.count;
  101. this.count = 0;
  102. this.diamond.opacity = 255;
  103. let DiamondIcon = cc.find('Canvas').getChildByName('UICamera').getChildByName('BelowTheMask').getChildByName('TopLabel').getChildByName('game-parent').getChildByName('Diamond').getChildByName('DiamondIcon');
  104. let Pos = DiamondIcon.parent.parent.parent.parent.convertToNodeSpaceAR(touchPos);
  105. this.diamond.parent = DiamondIcon.parent.parent.parent.parent;
  106. this.diamond.setPosition(Pos);
  107. let Duration = 0.5;
  108. var pos1 = DiamondIcon.convertToWorldSpaceAR(this.diamond.parent.getPosition());
  109. var pos2 = this.diamond.parent.convertToNodeSpaceAR(pos1);
  110. var MoveAnim = cc.moveTo(Duration,pos2);
  111. // var jump = cc.jumpBy(0.5, 0, 200, 100, 1);
  112. this.Colortag = true;
  113. var finished = cc.callFunc(function () {
  114. this.diamond.active = false;
  115. this.state = 0;
  116. this.diamondEnds(tempCount);
  117. task.taskZS();
  118. }.bind(this));
  119. var ScaleToAnim = cc.scaleTo(Duration, DiamondIcon.scale);
  120. var sequence = cc.sequence(cc.spawn(MoveAnim.easing(cc.easeOut(Duration)),ScaleToAnim), finished);
  121. this.diamond.runAction(sequence);
  122. },
  123. diamondEnds : function (count) {
  124. if (count >= this.diamondCount1 && count < this.diamondCount2) {
  125. GlobalD.GameData.PlusDiamond(10);
  126. }else if (count >= this.diamondCount2 && count < this.diamondCount3) {
  127. GlobalD.GameData.PlusDiamond(30);
  128. }else if (count>=this.diamondCount3) {
  129. GlobalD.GameData.PlusDiamond(50);
  130. }
  131. var tempNode = this.node;
  132. if (tempNode.getChildByName(this.diamond.name)!=null) {
  133. tempNode.getChildByName(this.diamond.name).destroy();
  134. }
  135. this._init();
  136. },
  137. /**
  138. * 得到一个节点的世界坐标
  139. * node的原点在中心
  140. * @param {*} node
  141. */
  142. localConvertWorldPointAR:function(node) {
  143. if (node) {
  144. return node.convertToWorldSpaceAR(cc.v2(0, 0));
  145. }
  146. return null;
  147. },
  148. /**
  149. * 得到一个节点的世界坐标
  150. * node的原点在左下边
  151. * @param {*} node
  152. */
  153. localConvertWorldPoint:function(node) {
  154. if (node) {
  155. return node.convertToWorldSpace(cc.v2(0, 0));
  156. }
  157. return null;
  158. },
  159. /**
  160. * 把一个世界坐标的点,转换到某个节点下的坐标
  161. * 原点在node中心
  162. * @param {*} node
  163. * @param {*} worldPoint
  164. */
  165. worldConvertLocalPointAR: function(node, worldPoint) {
  166. if (node) {
  167. return node.convertToNodeSpaceAR(worldPoint);
  168. }
  169. return null;
  170. },
  171. /**
  172. * 把一个世界坐标的点,转换到某个节点下的坐标
  173. * 原点在node左下角
  174. * @param {*} node
  175. * @param {*} worldPoint
  176. */
  177. worldConvertLocalPoint: function(node, worldPoint) {
  178. if (node) {
  179. return node.convertToNodeSpace(worldPoint);
  180. }
  181. return null;
  182. },
  183. /**
  184. * * 把一个节点的本地坐标转到另一个节点的本地坐标下
  185. * @param {*} node
  186. * @param {*} targetNode
  187. */
  188. convetOtherNodeSpace : function(node, targetNode) {
  189. if (!node || !targetNode) {
  190. return null;
  191. }
  192. //先转成世界坐标
  193. let worldPoint = this.localConvertWorldPoint(node);
  194. return this.worldConvertLocalPoint(targetNode, worldPoint);
  195. },
  196. /**
  197. * * 把一个节点的本地坐标转到另一个节点的本地坐标下
  198. * @param {*} node
  199. * @param {*} targetNode
  200. */
  201. convetOtherNodeSpaceAR: function(node, targetNode) {
  202. if (!node || !targetNode) {
  203. return null;
  204. }
  205. //先转成世界坐标
  206. let worldPoint = this.localConvertWorldPointAR(node);
  207. return this.worldConvertLocalPointAR(targetNode, worldPoint);
  208. },
  209. update (dt) {
  210. // this.setOpacity();
  211. },
  212. setOpacity : function(){
  213. if (this.diamond!=null) {
  214. if (this.Colortag) {
  215. if (this.diamond.opacity<=0) {
  216. this.Colortag = false;
  217. }else{
  218. this.diamond.opacity -=5;
  219. // console.log("都有什么 变了吗",this.coin);
  220. }
  221. }
  222. }
  223. },
  224. });