info.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. // Learn cc.Class:
  2. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
  3. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
  4. // Learn Attribute:
  5. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
  6. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
  7. // Learn life-cycle callbacks:
  8. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
  9. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
  10. cc.Class({
  11. extends: cc.Component,
  12. properties: {
  13. },
  14. // LIFE-CYCLE CALLBACKS:
  15. onLoad () {
  16. this._init();
  17. },
  18. _addInfo : function(callback){
  19. cc.loader.loadRes("prefab/Info", function (err, texture) {
  20. this.info = cc.instantiate(texture);
  21. if (this.node.getChildByName(this.info.name)!=null) {
  22. this.node.getChildByName(this.info.name).destroy();
  23. }
  24. this.node.addChild(this.info);
  25. this.info.x = this._x;
  26. this.info.y = this._y;
  27. this.info.active = false;
  28. if (callback != null) {
  29. callback();
  30. }
  31. }.bind(this));
  32. },
  33. _addCoin : function(parent,callback){
  34. cc.loader.loadRes("prefab/coin", function (err, texture) {
  35. this.coin = cc.instantiate(texture);
  36. var tempNode = this.node;
  37. if (parent!=null) {
  38. tempNode = parent;
  39. }
  40. // cc.log('要被添加跳钱的 node',tempNode);
  41. if (tempNode.getChildByName(this.coin.name)!=null) {
  42. tempNode.getChildByName(this.coin.name).destroy();
  43. }
  44. tempNode.addChild(this.coin);
  45. this.coin.x = this._x;
  46. this.coin.y = this._y;
  47. this.coin.active = false;
  48. if (callback != null) {
  49. callback();
  50. }
  51. }.bind(this));
  52. },
  53. _addSmoke1 : function(callback){
  54. cc.loader.loadRes("prefab/particles/smoke", function (err, texture) {
  55. this.smoke1 = cc.instantiate(texture);
  56. this.smoke1.name = "smoke1";
  57. var tempNode = this.node;
  58. if (tempNode.getChildByName(this.smoke1.name)!=null) {
  59. tempNode.getChildByName(this.smoke1.name).destroy();
  60. }
  61. tempNode.addChild(this.smoke1);
  62. this.smoke1.x = this._x;
  63. this.smoke1.y = this._y;
  64. this.smoke1.active = false;
  65. if (callback != null) {
  66. callback();
  67. }
  68. }.bind(this));
  69. },
  70. _addSmoke2 : function(callback){
  71. cc.loader.loadRes("prefab/particles/smoke", function (err, texture) {
  72. this.smoke2 = cc.instantiate(texture);
  73. this.smoke2.name = "smoke2";
  74. var tempNode = this.node;
  75. if (tempNode.getChildByName(this.smoke2.name)!=null) {
  76. tempNode.getChildByName(this.smoke2.name).destroy();
  77. }
  78. tempNode.addChild(this.smoke2);
  79. this.smoke2.x = this._x;
  80. this.smoke2.y = this._y;
  81. this.smoke2.active = false;
  82. if (callback != null) {
  83. callback();
  84. }
  85. }.bind(this));
  86. },
  87. start() {
  88. // this.play();
  89. },
  90. _init: function () {
  91. //0 未播放 1正播放
  92. this.state = 0;
  93. this._x = 50;
  94. this._y = 100;
  95. this._coinX = 0;
  96. this._coinY = 200;
  97. },
  98. startPlay: function () {
  99. // this.callbackCountDownTime = function () {
  100. //
  101. // this.unschedule(this.callbackCountDownTime);
  102. //
  103. // }
  104. // this.schedule(this.callbackCountDownTime, 1);
  105. //
  106. //
  107. // this.callbackLoadScene = function () {
  108. // // cc.director.loadScene("game");
  109. // }
  110. // this.scheduleOnce(this.callbackLoadScene, 1);
  111. },
  112. playCoinAnim : function(count,parent){
  113. // console.log("我点了金币了吗" ,this.state,count);
  114. if (this.state == 0) {
  115. this.state = 1;
  116. this._addCoin(parent,function () {
  117. this.coin.active = true;
  118. this.Colortag = false;
  119. this.coin.opacity =255;
  120. this.coin.x = this._coinX;
  121. this.coin.y = this._coinY;
  122. var Label = this.getNode("Label",this.coin);
  123. Label.getComponent(cc.Label).string = count;
  124. var jump = cc.jumpBy(2, 0, 100, 50, 1);
  125. this.Colortag = true;
  126. var finished = cc.callFunc(function () {
  127. this.callbackstop = function () {
  128. this.coin.active = false;
  129. this.coin.stopAllActions();
  130. this.state = 0;
  131. };
  132. this.scheduleOnce(this.callbackstop, 1);
  133. }.bind(this));
  134. var sequence = cc.sequence(jump, finished);
  135. this.coin.runAction(sequence);
  136. }.bind(this));
  137. }
  138. },
  139. setCoinColor : function(){
  140. if (this.coin!=null) {
  141. if (this.Colortag) {
  142. if (this.coin.opacity<=0) {
  143. this.CoinColortag = false;
  144. }else{
  145. this.coin.opacity -=2;
  146. // console.log("都有什么 变了吗",this.coin);
  147. }
  148. }
  149. }
  150. },
  151. setSmokeColor : function(){
  152. if (this.smoke1!=null) {
  153. if (this.Colortag1) {
  154. if (this.smoke1.opacity<=0) {
  155. this.Colortag1 = false;
  156. }else{
  157. this.smoke1.opacity -=2.5;
  158. // console.log("都有什么 变了吗",this.coin);
  159. }
  160. }
  161. }
  162. if (this.smoke2!=null) {
  163. if (this.Colortag2) {
  164. if (this.smoke2.opacity<=0) {
  165. this.Colortag2 = false;
  166. }else{
  167. this.smoke2.opacity -=2.5;
  168. // console.log("都有什么 变了吗",this.coin);
  169. }
  170. }
  171. }
  172. },
  173. //开始播放的方法
  174. play: function () {
  175. if (this.state == 0) {
  176. this.state = 1;
  177. this._addInfo(function () {
  178. this.info.active = true;
  179. this.info.x = this._x;
  180. this.info.y = this._y;
  181. var jump = cc.jumpBy(2, 0, 0, 20, 3);
  182. var finished = cc.callFunc(function () {
  183. this.callbackstop = function () {
  184. this._reStop();
  185. }
  186. this.scheduleOnce(this.callbackstop, 1);
  187. }.bind(this));
  188. var sequence = cc.sequence(jump, finished);
  189. this.info.runAction(sequence);
  190. }.bind(this));
  191. }
  192. },
  193. //停止播放的方法
  194. stop: function () {
  195. this.state = 0;
  196. if (this.info!=null) {
  197. this.info.active = false;
  198. this.info.stopAllActions();
  199. this.unscheduleAllCallbacks();
  200. if (this.node.getChildByName(this.info.name)!=null) {
  201. this.node.getChildByName(this.info.name).destroy();
  202. }
  203. }
  204. },
  205. //内部调用的方法
  206. _reStop : function(){
  207. this.info.active = false;
  208. this.info.stopAllActions();
  209. this.callbackrestop = function () {
  210. this.state = 0;
  211. this.play();
  212. }.bind(this);
  213. this.scheduleOnce(this.callbackrestop, 1);
  214. },
  215. //开始播放的方法
  216. playSmoke: function () {
  217. if (this.state == 0) {
  218. this.state = 1;
  219. this._addSmoke1(function () {
  220. this.smoke1.active = true;
  221. this.smoke1.opacity =255;
  222. this.smoke1.x = this._x;
  223. this.smoke1.y = this._y;
  224. this.Colortag1 = false;
  225. var sc = cc.scaleTo(1,1.2,1.2);
  226. this.smoke1.runAction(sc);
  227. var jump = cc.jumpBy(1, 0, 150, 50, 1);
  228. this.Colortag1 = true;
  229. var finished = cc.callFunc(function () {
  230. this.callbackstopSmoke = function () {
  231. this._reStopSmoke();
  232. }
  233. this.scheduleOnce(this.callbackstopSmoke, 0.1);
  234. }.bind(this));
  235. var sequence = cc.sequence(jump, finished);
  236. this.smoke1.runAction(sequence);
  237. this.scheduleOnce(function () {
  238. this._addSmoke2(function () {
  239. this.smoke2.active = true;
  240. this.smoke2.opacity =255;
  241. this.smoke2.x = this._x;
  242. this.smoke2.y = this._y;
  243. this.Colortag2 = false;
  244. var sc = cc.scaleTo(1,1.2,1.2);
  245. this.smoke2.runAction(sc);
  246. var jump = cc.jumpBy(1, 0, 150, 50, 1);
  247. this.Colortag2 = true;
  248. var finished = cc.callFunc(function () {
  249. this.smoke2.active = false;
  250. }.bind(this));
  251. var sequence = cc.sequence(jump, finished);
  252. this.smoke2.runAction(sequence);
  253. }.bind(this))
  254. }.bind(this),0.75);
  255. }.bind(this));
  256. }
  257. },
  258. //停止播放的方法
  259. stopSmoke: function () {
  260. this.state = 0;
  261. if (this.smoke1!=null) {
  262. this.smoke1.active = false;
  263. this.smoke1.stopAllActions();
  264. this.smoke2.active = false;
  265. this.smoke2.stopAllActions();
  266. this.unscheduleAllCallbacks();
  267. if (this.smoke1.getChildByName(this.smoke1.name)!=null) {
  268. this.smoke1.getChildByName(this.smoke1.name).destroy();
  269. }
  270. if (this.smoke2.getChildByName(this.smoke2.name)!=null) {
  271. this.smoke2.getChildByName(this.smoke2.name).destroy();
  272. }
  273. }
  274. },
  275. //内部调用的方法
  276. _reStopSmoke : function(){
  277. this.smoke1.active = false;
  278. this.smoke1.stopAllActions();
  279. this.callbackrestopSmoke = function () {
  280. this.state = 0;
  281. this.playSmoke();
  282. }.bind(this);
  283. this.scheduleOnce(this.callbackrestopSmoke, 1);
  284. },
  285. loadImg: function (container, url, w, h) {
  286. cc.loader.load(url, function (err, texture) {
  287. var sprite = new cc.SpriteFrame(texture);
  288. container.getComponent(cc.Sprite).spriteFrame = sprite;
  289. if (w != null) {
  290. container.width = w;
  291. }
  292. if (h != null) {
  293. container.height = h;
  294. }
  295. });
  296. },
  297. getNode: function (name, parent) {
  298. if (parent == null) {
  299. return this.node.getChildByName(name);
  300. } else {
  301. return parent.getChildByName(name);
  302. }
  303. },
  304. update (dt) {
  305. this.setCoinColor();
  306. // this.setSmokeColor();
  307. },
  308. });