buildingTips.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. },
  5. start() {
  6. this._init();
  7. // var buildingsInfo = this.node.getComponent("buildingsInfo");
  8. // console.log("buildingsInfo",buildingsInfo.buildInfo);
  9. // this.playShowStock(null,"x"+buildingsInfo.buildInfo._inventory);
  10. },
  11. _init: function () {
  12. //0 未播放 1正播放
  13. this.state = 0;
  14. this._x = 50;
  15. this._y = 100;
  16. this._coinX = 0;
  17. this._coinY = 200;
  18. },
  19. playCoinAnim: function (count, parent) {
  20. // console.log("我点了金币了吗" ,this.state,count);
  21. if (this.state == 0) {
  22. this.state = 1;
  23. this._addCoin(parent, function () {
  24. this.coin.active = true;
  25. this.Colortag = false;
  26. this.coin.opacity = 255;
  27. this.coin.x = 0;
  28. this.coin.y = this.node.height / 2;
  29. let Duration = 1;
  30. var Label = this.getNode("Label", this.coin);
  31. Label.getComponent(cc.Label).string = count;
  32. var scaleTo = cc.scaleTo(Duration, 1.5, 1.5);
  33. this.coin.runAction(scaleTo);
  34. // var jump = cc.jumpBy(Duration, 0, 0, 80, 1);
  35. let moveBy = cc.moveBy(Duration, 0, 100);
  36. let Fadeout = cc.fadeOut(Duration);
  37. this.Colortag = true;
  38. var finished = cc.callFunc(function () {
  39. this.callbackstop = function () {
  40. this.coin.active = false;
  41. this.coin.stopAllActions();
  42. this.state = 0;
  43. };
  44. this.scheduleOnce(this.callbackstop, 0.1);
  45. }.bind(this));
  46. var sequence = cc.sequence(cc.spawn(Fadeout, moveBy.easing(cc.easeIn(Duration / 2))), finished);
  47. this.coin.runAction(sequence);
  48. }.bind(this));
  49. }
  50. },
  51. //开始播放的方法
  52. playShowStock: function (src, string) {
  53. if (this.state == 0) {
  54. this.state = 1;
  55. this._addStock(function () {
  56. this.stock.active = true;
  57. this.stock.x = this._x;
  58. this.stock.y = this._y;
  59. if (src != null) {
  60. this.stock.getChildByName('img').getComponent(cc.Sprite).spriteFrame = src;
  61. }
  62. else {
  63. // this.stock.getChildByName('img').getComponent(cc.Sprite).spriteFrame = src;
  64. }
  65. //显示信息
  66. if (string != null) {
  67. this.stock.getChildByName('count').getComponent(cc.Label).string = string;
  68. this.TipName = string;
  69. }
  70. else {
  71. this.stock.getChildByName('count').getComponent(cc.Label).string = this.TipName;
  72. }
  73. var jump = cc.jumpBy(2, 0, 0, 20, 3);
  74. var finished = cc.callFunc(function () {
  75. this.callbackstopShowStock = function () {
  76. this._reStopShowStock();
  77. }
  78. this.scheduleOnce(this.callbackstopShowStock, 1);
  79. }.bind(this));
  80. var sequence = cc.sequence(jump, finished);
  81. this.stock.runAction(sequence);
  82. }.bind(this));
  83. }
  84. },
  85. setShowStockData: function (src, string) {
  86. if (src != null) {
  87. this.stock.getChildByName('img').getComponent(cc.Sprite).spriteFrame = src;
  88. }
  89. else {
  90. // this.stock.getChildByName('img').getComponent(cc.Sprite).spriteFrame = src;
  91. }
  92. //显示信息
  93. if (string != null) {
  94. this.stock.getChildByName('count').getComponent(cc.Label).string = string;
  95. this.TipName = string;
  96. }
  97. else {
  98. this.stock.getChildByName('count').getComponent(cc.Label).string = this.TipName;
  99. }
  100. },
  101. //内部调用的方法
  102. _reStopShowStock: function () {
  103. this.stock.active = false;
  104. this.stock.stopAllActions();
  105. this.callbackrestop = function () {
  106. this.state = 0;
  107. this.playShowStock();
  108. }.bind(this);
  109. this.scheduleOnce(this.callbackrestop, 1);
  110. },
  111. //停止播放的方法
  112. stopShowStockAnim: function () {
  113. this.state = 0;
  114. if (this.stock != null) {
  115. this.stock.active = false;
  116. this.stock.stopAllActions();
  117. this.unscheduleAllCallbacks();
  118. if (this.node.getChildByName(this.stock.name) != null) {
  119. this.node.getChildByName(this.stock.name).destroy();
  120. }
  121. }
  122. },
  123. _addCoin: function (parent, callback) {
  124. cc.loader.loadRes("prefab/coin", function (err, texture) {
  125. this.coin = cc.instantiate(texture);
  126. var tempNode = this.node;
  127. if (parent != null) {
  128. tempNode = parent;
  129. }
  130. // cc.log('要被添加跳钱的 node',tempNode);
  131. if (tempNode.getChildByName(this.coin.name) != null) {
  132. tempNode.getChildByName(this.coin.name).destroy();
  133. }
  134. tempNode.addChild(this.coin);
  135. this.coin.x = this._x;
  136. this.coin.y = this._y;
  137. this.coin.active = false;
  138. if (callback != null) {
  139. callback();
  140. }
  141. }.bind(this));
  142. },
  143. _addSellOut: function (callback) {
  144. cc.loader.loadRes("prefab/sellout", function (err, texture) {
  145. this.sellout = cc.instantiate(texture);
  146. var tempNode = this.node;
  147. // cc.log('要被添加跳钱的 node',tempNode);
  148. if (tempNode.getChildByName(this.sellout.name) != null) {
  149. tempNode.getChildByName(this.sellout.name).destroy();
  150. }
  151. tempNode.addChild(this.sellout);
  152. this.sellout.x = this._x;
  153. this.sellout.y = this._y;
  154. this.sellout.active = false;
  155. if (callback != null) {
  156. callback();
  157. }
  158. }.bind(this));
  159. },
  160. _addBoard: function (callback) {
  161. cc.loader.loadRes("prefab/board", function (err, texture) {
  162. this.board = cc.instantiate(texture);
  163. var tempNode = this.node;
  164. // cc.log('要被添加跳钱的 node',tempNode);
  165. if (tempNode.getChildByName(this.board.name) != null) {
  166. tempNode.getChildByName(this.board.name).destroy();
  167. }
  168. tempNode.addChild(this.board);
  169. this.board.x = this._x;
  170. this.board.y = this._y;
  171. this.board.active = false;
  172. if (callback != null) {
  173. callback();
  174. }
  175. }.bind(this));
  176. },
  177. _addSmoke1: function (callback) {
  178. cc.loader.loadRes("prefab/particles/smoke", function (err, texture) {
  179. this.smoke1 = cc.instantiate(texture);
  180. this.smoke1.name = "smoke1";
  181. var tempNode = this.node;
  182. if (tempNode.getChildByName(this.smoke1.name) != null) {
  183. tempNode.getChildByName(this.smoke1.name).destroy();
  184. }
  185. tempNode.addChild(this.smoke1);
  186. this.smoke1.x = this._x;
  187. this.smoke1.y = this._y;
  188. this.smoke1.active = false;
  189. if (callback != null) {
  190. callback();
  191. }
  192. }.bind(this));
  193. },
  194. _addSmoke2: function (callback) {
  195. cc.loader.loadRes("prefab/particles/smoke", function (err, texture) {
  196. this.smoke2 = cc.instantiate(texture);
  197. this.smoke2.name = "smoke2";
  198. var tempNode = this.node;
  199. if (tempNode.getChildByName(this.smoke2.name) != null) {
  200. tempNode.getChildByName(this.smoke2.name).destroy();
  201. }
  202. tempNode.addChild(this.smoke2);
  203. this.smoke2.x = this._x;
  204. this.smoke2.y = this._y;
  205. this.smoke2.active = false;
  206. if (callback != null) {
  207. callback();
  208. }
  209. }.bind(this));
  210. },
  211. _addStock: function (callback) {
  212. cc.loader.loadRes("prefab/stock", function (err, texture) {
  213. this.stock = cc.instantiate(texture);
  214. this.stock.name = "stock";
  215. var tempNode = this.node;
  216. if (tempNode.getChildByName(this.stock.name) != null) {
  217. tempNode.getChildByName(this.stock.name).destroy();
  218. }
  219. tempNode.addChild(this.stock);
  220. this.stock.x = this._x;
  221. this.stock.y = this._y;
  222. this.stock.active = false;
  223. if (callback != null) {
  224. callback();
  225. }
  226. }.bind(this));
  227. },
  228. setCoinColor: function () {
  229. if (this.coin != null) {
  230. if (this.Colortag) {
  231. if (this.coin.opacity <= 0) {
  232. this.CoinColortag = false;
  233. } else {
  234. this.coin.opacity -= 2;
  235. // console.log("都有什么 变了吗",this.coin);
  236. }
  237. }
  238. }
  239. },
  240. setSmokeColor: function () {
  241. if (this.smoke1 != null) {
  242. if (this.Colortag1) {
  243. if (this.smoke1.opacity <= 0) {
  244. this.Colortag1 = false;
  245. } else {
  246. this.smoke1.opacity -= 2.5;
  247. // console.log("都有什么 变了吗",this.coin);
  248. }
  249. }
  250. }
  251. if (this.smoke2 != null) {
  252. if (this.Colortag2) {
  253. if (this.smoke2.opacity <= 0) {
  254. this.Colortag2 = false;
  255. } else {
  256. this.smoke2.opacity -= 2.5;
  257. // console.log("都有什么 变了吗",this.coin);
  258. }
  259. }
  260. }
  261. },
  262. //开始播放的方法
  263. playSmokeAnim: function () {
  264. // cc.log('开始冒烟');
  265. if (this.state == 0) {
  266. this.state = 1;
  267. this._addSmoke1(function () {
  268. this.smoke1.active = true;
  269. this.smoke1.opacity = 255;
  270. this.smoke1.x = this._x;
  271. this.smoke1.y = this._y;
  272. this.Colortag1 = false;
  273. var sc = cc.scaleTo(1, 1.2, 1.2);
  274. this.smoke1.runAction(sc);
  275. var jump = cc.jumpBy(1, 0, 150, 50, 1);
  276. this.Colortag1 = true;
  277. var finished = cc.callFunc(function () {
  278. this.callbackstopSmoke = function () {
  279. this._reStopSmoke();
  280. }
  281. this.scheduleOnce(this.callbackstopSmoke, 0.1);
  282. }.bind(this));
  283. var sequence = cc.sequence(jump, finished);
  284. this.smoke1.runAction(sequence);
  285. this.scheduleOnce(function () {
  286. this._addSmoke2(function () {
  287. this.smoke2.active = true;
  288. this.smoke2.opacity = 255;
  289. this.smoke2.x = this._x;
  290. this.smoke2.y = this._y;
  291. this.Colortag2 = false;
  292. var sc = cc.scaleTo(1, 1.2, 1.2);
  293. this.smoke2.runAction(sc);
  294. var jump = cc.jumpBy(1, 0, 150, 50, 1);
  295. this.Colortag2 = true;
  296. var finished = cc.callFunc(function () {
  297. this.smoke2.active = false;
  298. }.bind(this));
  299. var sequence = cc.sequence(jump, finished);
  300. this.smoke2.runAction(sequence);
  301. }.bind(this))
  302. }.bind(this), 0.75);
  303. }.bind(this));
  304. }
  305. },
  306. //停止播放的方法
  307. stopSmokeAnim: function () {
  308. // cc.log('停止冒烟');
  309. this.state = 0;
  310. if (this.smoke1 != null) {
  311. this.smoke1.active = false;
  312. this.smoke1.stopAllActions();
  313. this.smoke2.active = false;
  314. this.smoke2.stopAllActions();
  315. this.unscheduleAllCallbacks();
  316. if (this.smoke1.getChildByName(this.smoke1.name) != null) {
  317. this.smoke1.getChildByName(this.smoke1.name).destroy();
  318. }
  319. if (this.smoke2.getChildByName(this.smoke2.name) != null) {
  320. this.smoke2.getChildByName(this.smoke2.name).destroy();
  321. }
  322. }
  323. },
  324. //内部调用的方法
  325. _reStopSmoke: function () {
  326. this.smoke1.active = false;
  327. this.smoke1.stopAllActions();
  328. this.callbackrestopSmoke = function () {
  329. this.state = 0;
  330. this.playSmokeAnim();
  331. }.bind(this);
  332. this.scheduleOnce(this.callbackrestopSmoke, 1);
  333. },
  334. //开始播放的方法
  335. playSellOutAnim: function () {
  336. if (this.state == 0) {
  337. this.state = 1;
  338. this._addSellOut(function () {
  339. this.sellout.active = true;
  340. this.sellout.x = this._x;
  341. this.sellout.y = this._y;
  342. var jump = cc.jumpBy(3, 0, 0, 20, 3);
  343. var finished = cc.callFunc(function () {
  344. this.callbackstopSellOut = function () {
  345. // this._reStopSellOut();
  346. this.stopSellOutAnim();
  347. }
  348. this.scheduleOnce(this.callbackstopSellOut, 2);
  349. }.bind(this));
  350. var sequence = cc.sequence(jump, finished);
  351. this.sellout.runAction(sequence);
  352. }.bind(this));
  353. }
  354. },
  355. //内部调用的方法
  356. _reStopSellOut: function () {
  357. this.sellout.active = false;
  358. this.sellout.stopAllActions();
  359. this.callbackrestopSellOut = function () {
  360. this.state = 0;
  361. this.playSellOutAnim();
  362. }.bind(this);
  363. this.scheduleOnce(this.callbackrestopSellOut, 1);
  364. },
  365. //停止播放的方法
  366. stopSellOutAnim: function () {
  367. this.state = 0;
  368. if (this.sellout != null) {
  369. this.sellout.active = false;
  370. this.sellout.stopAllActions();
  371. this.unscheduleAllCallbacks();
  372. if (this.node.getChildByName(this.sellout.name) != null) {
  373. this.node.getChildByName(this.sellout.name).destroy();
  374. }
  375. }
  376. },
  377. //开始播放的方法
  378. playBoard: function () {
  379. if (this.state == 0) {
  380. this.state = 1;
  381. this._addBoard(function () {
  382. this.board.active = true;
  383. this.board.x = 0;
  384. this.board.y = 100;
  385. var sc1 = cc.scaleTo(0.25, 1.2, 1.2);
  386. var sc2 = cc.scaleTo(0.25, 1, 1);
  387. var scc = cc.sequence(sc1, sc2);
  388. this.board.runAction(scc);
  389. var jump = cc.jumpBy(0.5, 0, 0, 20, 1);
  390. var finished = cc.callFunc(function () {
  391. // this.callbackstopSellOut = function () {
  392. // // this._reStopSellOut();
  393. // // this.stopSellOutAnim();
  394. // }
  395. // this.scheduleOnce(this.callbackstopSellOut, 2);
  396. }.bind(this));
  397. var sequence = cc.sequence(jump, finished);
  398. this.board.runAction(sequence);
  399. }.bind(this));
  400. }
  401. },
  402. //停止播放的方法
  403. stopBoard: function () {
  404. this.state = 0;
  405. if (this.board != null) {
  406. this.board.active = false;
  407. this.board.stopAllActions();
  408. this.unscheduleAllCallbacks();
  409. if (this.node.getChildByName(this.board.name) != null) {
  410. this.node.getChildByName(this.board.name).destroy();
  411. }
  412. }
  413. },
  414. getNode: function (name, parent) {
  415. if (parent == null) {
  416. return this.node.getChildByName(name);
  417. } else {
  418. return parent.getChildByName(name);
  419. }
  420. },
  421. loadImg: function (container, url, w, h) {
  422. cc.loader.load(url, function (err, texture) {
  423. var sprite = new cc.SpriteFrame(texture);
  424. container.getComponent(cc.Sprite).spriteFrame = sprite;
  425. if (w != null) {
  426. container.width = w;
  427. }
  428. if (h != null) {
  429. container.height = h;
  430. }
  431. });
  432. },
  433. //开始播放睡觉动画
  434. playSleepAnim: function () {
  435. if (this.state == 0) {
  436. this.state = 1;
  437. this._sleep(function () {
  438. this.sleep.active = true;
  439. this.sleep.x = 0;
  440. this.sleep.y = 100;
  441. var sc1 = cc.scaleTo(0.25, 1.2, 1.2);
  442. var sc2 = cc.scaleTo(0.25, 1, 1);
  443. var scc = cc.sequence(sc1, sc2);
  444. this.sleep.runAction(scc);
  445. var jump = cc.jumpBy(0.5, 0, 0, 20, 1);
  446. var finished = cc.callFunc(function () {
  447. }.bind(this));
  448. var sequence = cc.sequence(jump, finished);
  449. this.sleep.runAction(sequence);
  450. }.bind(this));
  451. }
  452. },
  453. //停止播放的方法
  454. stopSleepAnim: function () {
  455. this.state = 0;
  456. if (this.sleep != null) {
  457. this.sleep.active = false;
  458. this.sleep.stopAllActions();
  459. this.unscheduleAllCallbacks();
  460. if (this.node.getChildByName(this.sleep.name) != null) {
  461. this.node.getChildByName(this.sleep.name).destroy();
  462. }
  463. }
  464. },
  465. _sleep: function (callback) {
  466. cc.loader.loadRes("prefab/particles/sleep", function (err, texture) {
  467. this.sleep = cc.instantiate(texture);
  468. var tempNode = this.node;
  469. if (tempNode.getChildByName(this.sleep.name) != null) {
  470. tempNode.getChildByName(this.sleep.name).destroy();
  471. }
  472. tempNode.addChild(this.sleep);
  473. this.sleep.x = this._x;
  474. this.sleep.y = this._y;
  475. this.sleep.active = false;
  476. if (callback != null) {
  477. callback();
  478. }
  479. }.bind(this));
  480. },
  481. });