ZY_HomeScene.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. var ZYGameTool = require('GameTool');
  2. cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. clickFlag: true,
  6. homeButtons: {type: cc.Button, default: []},
  7. buttonsName: null,
  8. coinLabel: cc.Label,
  9. bgmClip: {type: cc.AudioClip, default: null}, //背景音乐
  10. logoNode: cc.Node,
  11. cactursNode: cc.Node,
  12. maskLayer: cc.Prefab,
  13. settingLayer: cc.Prefab,
  14. helpLayer: cc.Prefab,
  15. coinNode: cc.Node,
  16. },
  17. // LIFE-CYCLE CALLBACKS:
  18. onLoad() {
  19. this.setLocalDataItem();
  20. this.buttonsName = {redBagBtn: 0, giftBtn: 1, redBtn: 2, startBtn: 3, settingBtn: 4, helpBtn: 5, signBtn: 6};
  21. cc.debug.setDisplayStats(false);
  22. //首页按钮注册监听
  23. for (var i in this.homeButtons) {
  24. this.homeButtons[i].node.scale = 0;
  25. this.homeButtons[i].node.on("click", this.gameClickCall, this);
  26. }
  27. this.homeEffect();
  28. },
  29. start() {
  30. if (isFirstInGame) {
  31. ZYGameTool.getInstance().loadSpriteFrame(function () {
  32. cc.director.preloadScene("mainScene");
  33. isFirstInGame = false;
  34. this.init();
  35. }.bind(this));
  36. } else {
  37. this.init();
  38. }
  39. ZYGameTool.getInstance().OpenAd("banner", "banner");
  40. },
  41. init: function () {
  42. this.setCoinLabel();
  43. //播放背景声音
  44. zy.playBgMusic(this.bgmClip, true)
  45. },
  46. /**
  47. * 初始化设置游戏所需本地化数据
  48. */
  49. setLocalDataItem: function () {
  50. coinNum = ZYGameTool.getInstance().getIntegerForKey(localDataItem.coinNum, 100);//默认金币
  51. playeSound = ZYGameTool.getInstance().getIntegerForKey(localDataItem.playeSound, 1);//音效开关
  52. playeMusic = ZYGameTool.getInstance().getIntegerForKey(localDataItem.playeMusic, 1);//背景音乐开关
  53. console.log("--playeMusic-: ", playeMusic);
  54. console.log("--playeSound-: ", playeSound);
  55. },
  56. /**
  57. *设置金币
  58. */
  59. setCoinLabel: function () {
  60. if (coinNum === undefined || coinNum === 'NAN') coinNum = 0;
  61. this.coinLabel.string = coinNum;
  62. },
  63. /**
  64. * 首页的动画
  65. */
  66. homeEffect: function () {
  67. var logoHeight = 1000;
  68. var initLogoHeight = this.logoNode.parent.y;
  69. {
  70. this.logoNode.y += logoHeight;
  71. this.cactursNode.y += logoHeight;
  72. }
  73. //logo
  74. {
  75. var actions = [];
  76. var ac1 = cc.moveBy(0.2, cc.v2(0, logoHeight * -1 - 50));
  77. var ac2 = cc.moveBy(0.1, cc.v2(0, 50));
  78. var ac3 = cc.scaleTo(0.1, 1, 0.6);
  79. var ac4 = cc.scaleTo(0.1, 1, 1);
  80. actions.push(ac1);
  81. actions.push(ac2);
  82. actions.push(ac3);
  83. actions.push(ac4);
  84. // actions2.concat(0,actions.length-1);
  85. var actions2 = [].concat(actions);
  86. console.log("actions2", actions2);
  87. this.logoNode.runAction(cc.sequence(
  88. cc.delayTime(0.2),
  89. cc.moveBy(0.2, cc.v2(0, logoHeight * -1 - 50)),
  90. cc.moveBy(0.1, cc.v2(0, 50)),
  91. cc.scaleTo(0.1, 1, 0.6),
  92. cc.scaleTo(0.1, 1, 1),
  93. cc.delayTime(1.5),
  94. cc.callFunc(function (node) {
  95. node.runAction(cc.repeatForever(
  96. cc.sequence(
  97. cc.rotateTo(1.5, 1),
  98. cc.rotateTo(1.5, -1),
  99. )
  100. ))
  101. }.bind(this), this.constructor)
  102. ));
  103. this.cactursNode.runAction(cc.sequence(
  104. cc.delayTime(0.35 + 0.2),
  105. cc.moveBy(0.2, cc.v2(0, logoHeight * -1 - 50)),
  106. cc.moveBy(0.1, cc.v2(0, 50)),
  107. cc.scaleTo(0.1, 1, 0.6),
  108. cc.scaleTo(0.1, 1, 1),
  109. cc.delayTime(0.5),
  110. cc.callFunc(function (node) {
  111. node.runAction(cc.repeatForever(
  112. cc.sequence(
  113. cc.scaleTo(1.5, 1, 0.85),
  114. cc.scaleTo(1.5, 1, 1),
  115. )
  116. ))
  117. }.bind(this), this.constructor)
  118. ));
  119. }
  120. //按钮统一动画
  121. {
  122. this.node.runAction(cc.sequence(
  123. cc.delayTime(0.6 + 0.2),
  124. cc.callFunc(function () {
  125. var dl = 0.05;
  126. for (var i in this.homeButtons) {
  127. this.homeButtons[i].node.runAction(cc.sequence(
  128. cc.delayTime(dl * i),
  129. cc.scaleTo(0.5, 1, 1),
  130. ));
  131. }
  132. }.bind(this))
  133. ));
  134. this.homeButtons[this.buttonsName.startBtn].node.runAction(cc.sequence(cc.delayTime(1.8), cc.callFunc(function (node) {
  135. node.runAction(cc.repeatForever(
  136. cc.sequence(
  137. cc.scaleTo(2.5, 0.85),
  138. cc.scaleTo(2.5, 1),
  139. )
  140. ))
  141. }.bind(this), this.homeButtons[this.buttonsName.startBtn])))
  142. }
  143. {
  144. this.homeButtons[this.buttonsName.redBtn].node.runAction(cc.sequence(cc.delayTime(1.8), cc.callFunc(function (node) {
  145. node.runAction(cc.repeatForever(
  146. cc.sequence(cc.spawn(cc.rotateTo(0.8, 7), cc.scaleTo(0.8, 0.9)), cc.spawn(cc.rotateTo(0.8, -7), cc.scaleTo(0.8, 1.0)))
  147. ))
  148. }.bind(this), this.homeButtons[this.buttonsName.redBtn])));
  149. var finger = this.homeButtons[this.buttonsName.redBtn].node.parent.getChildByName("finger");
  150. finger.runAction(cc.sequence(cc.delayTime(1.8),cc.scaleTo(0.5, 0.6), cc.callFunc(function (node) {
  151. node.runAction( cc.repeatForever(
  152. cc.sequence(
  153. cc.show(),
  154. cc.blink(2, 5),
  155. cc.delayTime(2),
  156. cc.hide(),
  157. cc.delayTime(5),
  158. )
  159. ))
  160. }.bind(this), finger)));
  161. }
  162. },
  163. gameClickCall: function (target) {
  164. var name = target.node.name;
  165. console.log("--name--: ", name);
  166. if (name === "startBtn") {
  167. this.gameStartClick();
  168. } else if (name === "helpBtn") {
  169. this.gameHelpClick(10);
  170. } else if (name === "redBtn") {
  171. this.gameRedClick();
  172. } else if (name === "settingBtn") {
  173. this.gameSettingClick();
  174. }
  175. },
  176. gameHelpClick: function () {
  177. var helpLayer = cc.instantiate(this.helpLayer);
  178. helpLayer.parent = this.node;
  179. helpLayer.zIndex = 1000;
  180. },
  181. gameSettingClick: function () {
  182. var settingNode = cc.instantiate(this.settingLayer);
  183. settingNode.parent = this.node;
  184. settingNode.zIndex = 1000;
  185. },
  186. gameRedClick: function (pos) {
  187. ZYGameTool.getInstance().OpenAd("home_lqjb", "video",
  188. function () {
  189. this.coinAction(50);
  190. coinNum += 10;
  191. this.setCoinLabel();
  192. ZYGameTool.getInstance().setIntegerForKey(localDataItem.coinNum, coinNum);
  193. console.log("---debug---成功---");
  194. }.bind(this),
  195. function () {
  196. console.log("---debug---失败---");
  197. }.bind(this),
  198. )
  199. },
  200. coinAction: function (num) {
  201. var dl = 0.15;
  202. var bezier = [cc.v2(0, 0), cc.v2(-300, 100), cc.v2(this.coinNode.x - 60, this.coinNode.y)];
  203. for (var i = 0; i < num; i++) {
  204. var node = zy.getSprite("common/coin");
  205. node.setPosition(cc.v2(0, 0));
  206. node.scale = 0;
  207. node.zIndex = 100 - i;
  208. this.node.addChild(node);
  209. node.runAction(cc.speed(cc.sequence(cc.delayTime(dl * i), cc.scaleTo(0.2, 1, 1), cc.delayTime(0.1), cc.bezierTo(0.2, bezier), cc.fadeOut(0.05), cc.callFunc(function (node) {
  210. node.removeFromParent();
  211. }.bind(this), node)), num / 10));
  212. }
  213. },
  214. gameStartClick: function () {
  215. if (!this.clickFlag) return;
  216. zy.stopBgMusic();
  217. this.clickFlag = false;
  218. cc.director.loadScene("mainScene");
  219. // this.node.runAction(cc.fadeOut(0.8));
  220. // this.node.runAction(cc.sequence(cc.delayTime(0.5), cc.callFunc(function () {
  221. // cc.director.loadScene("mainScene");
  222. // })));
  223. },
  224. // update (dt) {},
  225. onDestroy() {
  226. //关闭所有音乐、音效
  227. cc.audioEngine.stopAll();
  228. //移除所有事件监听
  229. cc.game.targetOff(this);
  230. }
  231. });