GameSence.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. var cfg = require("Configure")
  2. var WxCommon = require("../WxCommon")
  3. cc.Class({
  4. extends: cc.Component,
  5. properties: {
  6. buDing: {
  7. default: null,
  8. type: cc.Sprite
  9. },
  10. obstacleNode: {
  11. default: null,
  12. type: cc.Node
  13. },
  14. scoreNode: {
  15. default: null,
  16. type: cc.Node
  17. },
  18. funcNode: {
  19. default: null,
  20. type: cc.Node
  21. },
  22. sumScore: 0,
  23. layerNum: 0,
  24. obstacleNum: 0,
  25. starNum: 0,
  26. gamebgMusic: {
  27. default: null,
  28. url: cc.AudioClip
  29. },
  30. changeColorAudio: {
  31. default: null,
  32. url: cc.AudioClip
  33. },
  34. eatGoldAudio: {
  35. default: null,
  36. url: cc.AudioClip
  37. },
  38. newRecordAudio: {
  39. default: null,
  40. url: cc.AudioClip
  41. },
  42. buttonAudio: {
  43. default: null,
  44. url: cc.AudioClip
  45. },
  46. passNode: { //超越提示节点
  47. default: null,
  48. type: cc.Node
  49. },
  50. bShare: false,
  51. bDead: false,
  52. bPause: false,
  53. feiendtex: null,
  54. childCanvas: null
  55. },
  56. onLoad: function () {
  57. if (cc.vv == undefined) {
  58. cc.vv = {};
  59. }
  60. var manager = cc.director.getCollisionManager();
  61. manager.enabled = true;
  62. this.registerTouchEventHandle();
  63. if (cc.winSize.height > 1334) {
  64. this.node.getChildByName("ScoreNode").setPosition(0, (cc.winSize.height - 1334) / 2);
  65. this.node.getChildByName("FuncNode").setPosition(0, -(cc.winSize.height - 1334) / 2);
  66. }
  67. this.showVd = false;
  68. this.debugErrorTips = false;
  69. },
  70. start: function () {
  71. this.feiendtex = new cc.Texture2D();
  72. this.childCanvas = new cc.Texture2D();
  73. },
  74. startGame: function () {
  75. //通知子域重新填充rankList
  76. if (UserInfo.platform == 1 && cc.vv.isRank) {
  77. wx.postMessage({
  78. method: 'fillRankList'
  79. });
  80. }
  81. cc.audioEngine.playMusic(this.gamebgMusic, true);
  82. this.sumScore = 0;
  83. this.layerNum = 0;
  84. this.starNum = 0;
  85. this.obstacleNum = 0;
  86. this.bShare = true;
  87. this.bDead = false;
  88. this.bVideo = true;
  89. this.scoreNode.active = true;
  90. this.funcNode.active = true;
  91. this.obstacleNode.removeAllChildren();
  92. this.node.getChildByName("Camera").setPosition(cc.p(0, 0));
  93. var sumScoreLabel = this.scoreNode.getChildByName("ScoreLabel");
  94. sumScoreLabel.getComponent("cc.Label").string = this.sumScore;
  95. //var layerNumLabel = this.scoreNode.getChildByName("LayerLabel");
  96. //layerNumLabel.getComponent("cc.Label").string = this.layerNum;
  97. this.node.getChildByName("Camera").getComponent("CameraControl").resetData();
  98. this.buDing.getComponent("BuDingSprite").initBuding();
  99. //cc.director.getPhysicsManager().enabled = true;
  100. this.createObstacle();
  101. this.node.runAction(cc.sequence(cc.delayTime(1), cc.callFunc(this.createObstacle, this), cc.delayTime(1), cc.callFunc(this.createObstacle, this)));
  102. let stmp = new Date().getTime();
  103. cc.loader.load({
  104. url: "https://pub.dwstatic.com/wxgame/budingGame/share.json?" + (stmp/100000).toFixed(0),
  105. type: "json"
  106. }, (err, res) => {
  107. if(res) {
  108. this.showVd = res.showVd
  109. this.bShare = res.bShare
  110. this.debugErrorTips = res.debugErrorTips
  111. }
  112. })
  113. },
  114. finishGame: function () {
  115. this.bDead = true;
  116. this.buDing.node.active = false;
  117. this.buDing.node.getComponent("BuDingSprite").setSkSpriteVisible(false);
  118. this.scoreNode.active = false;
  119. this.funcNode.active = false;
  120. var gameOverNode = this.node.getChildByName("GameOver");
  121. gameOverNode.active = true;
  122. var endLayerNumLabel = gameOverNode.getChildByName("EndLayerNumLabel");
  123. endLayerNumLabel.getComponent("cc.Label").string = this.sumScore;
  124. if (this.bShare) {
  125. // var endScoreLabel = gameOverNode.getChildByName("ShareNode").getChildByName("EndScoreLabel");
  126. // endScoreLabel.getComponent("cc.Label").string = this.sumScore;
  127. var shareNode = gameOverNode.getChildByName("ShareNode");
  128. shareNode.active = true;
  129. gameOverNode.getChildByName("EndNode").active = false;
  130. if (this.showVd) {
  131. //显示观看视频广告按钮
  132. shareNode.getChildByName("ShareBtn").active = false;
  133. shareNode.getChildByName("VideoBtn").active = true;
  134. } else {
  135. //显示邀请好友按钮
  136. shareNode.getChildByName("ShareBtn").active = true;
  137. shareNode.getChildByName("VideoBtn").active = false;
  138. }
  139. this.startFinishCount();
  140. } else {
  141. gameOverNode.getChildByName("EndNode").active = true;
  142. gameOverNode.getChildByName("ShareNode").active = false;
  143. this.gameOver();
  144. var data = [{
  145. key: UserInfo.getCurWeekScoreValue(),
  146. value: UserInfo.getScore().toString()
  147. }, {
  148. key: "driLevel",
  149. value: '0'
  150. }
  151. ];
  152. if (CC_WECHATGAME && cc.vv.isRank) {
  153. this.node.parent.getChildByName("PHB").getChildByName("bg").active = false;
  154. this.showRank();
  155. this.updateXYXScore('showMyRank', data);
  156. }
  157. }
  158. },
  159. //结束界面开始倒计时
  160. startFinishCount: function (countDown) {
  161. var countDown = 10;
  162. var gameOverNode = this.node.getChildByName("GameOver");
  163. var countDownLabel = gameOverNode.getChildByName("ShareNode").getChildByName("EndCountDownLabel");
  164. countDownLabel.getComponent("cc.Label").string = countDown;
  165. var callback = function () {
  166. if (countDown <= 0) {
  167. gameOverNode.getChildByName("ShareNode").active = false;
  168. gameOverNode.getChildByName("EndNode").active = true;
  169. this.gameOver();
  170. var data = [{
  171. key: UserInfo.getCurWeekScoreValue(),
  172. value: UserInfo.getScore().toString()
  173. }, {
  174. key: "driLevel",
  175. value: '0'
  176. }
  177. ];
  178. if (CC_WECHATGAME && cc.vv.isRank) {
  179. this.showRank();
  180. self.updateXYXScore('showMyRank', data);
  181. }
  182. } else {
  183. countDown--;
  184. countDownLabel.getComponent("cc.Label").string = countDown;
  185. countDownLabel.runAction(cc.sequence(cc.delayTime(1), cc.callFunc(callback, this)));
  186. }
  187. }
  188. countDownLabel.runAction(cc.sequence(cc.delayTime(1), cc.callFunc(callback, this)));
  189. },
  190. gameOver: function () {
  191. var gameOverNode = this.node.getChildByName("GameOver");
  192. var historyScoreLabel = gameOverNode.getChildByName("EndNode").getChildByName("HistoryScoreLabel");
  193. var newRecordSp = gameOverNode.getChildByName("EndNode").getChildByName("NewRecordSp");
  194. if (this.sumScore > UserInfo.getHistoryScore()) {
  195. newRecordSp.active = true;
  196. UserInfo.setString("historyScore", this.sumScore);
  197. cc.audioEngine.playEffect(this.newRecordAudio);
  198. newRecordSp.runAction(cc.repeatForever(cc.sequence(cc.scaleTo(0.5, 1.5), cc.scaleTo(0.5, 1))));
  199. } else {
  200. newRecordSp.active = false;
  201. }
  202. if (this.sumScore > UserInfo.getScore()) {
  203. UserInfo.setScore(this.sumScore);
  204. }
  205. historyScoreLabel.getComponent("cc.Label").string = "历史最高分:" + UserInfo.getHistoryScore();
  206. UserInfo.addGold(this.sumScore);
  207. },
  208. continueGame: function () {
  209. this.bShare = false;
  210. this.bDead = false;
  211. this.layerNum = 0;
  212. this.starNum = 0;
  213. this.obstacleNum = 0;
  214. this.scoreNode.active = true;
  215. this.funcNode.active = true;
  216. this.node.getChildByName("GameOver").active = false;
  217. this.obstacleNode.removeAllChildren();
  218. this.node.getChildByName("Camera").getComponent("CameraControl").resetData();
  219. this.node.getChildByName("Camera").setPosition(cc.p(0, 0));
  220. this.buDing.getComponent("BuDingSprite").initBuding();
  221. //cc.director.getPhysicsManager().enabled = true;
  222. this.createObstacle();
  223. this.node.runAction(cc.sequence(cc.delayTime(1), cc.callFunc(this.createObstacle, this), cc.delayTime(1), cc.callFunc(this.createObstacle, this)));
  224. },
  225. createObstacle: function () {
  226. var obstacleType = this.randomObstacleType();
  227. //根据当前分数控制障碍物之间的高度
  228. var disObstacleY;
  229. if (this.sumScore > 40) {
  230. disObstacleY = 0;
  231. } else if (this.sumScore > 30 && this.sumScore <= 40) {
  232. disObstacleY = 50;
  233. } else if (this.sumScore > 15 && this.sumScore <= 30) {
  234. disObstacleY = 100;
  235. } else {
  236. disObstacleY = 150;
  237. }
  238. var self = this;
  239. cc.loader.loadRes(cfg.obstacleTypePath[obstacleType], function (err, prefab) {
  240. if (self.obstacleNode.getChildByName("obstacle1") == null) {
  241. var obstacle1 = cc.instantiate(prefab);
  242. obstacle1.setName("obstacle1");
  243. self.obstacleNode.addChild(obstacle1);
  244. if (self.obstacleNode.getChildByName("obstacle3") != null) {
  245. var type = self.obstacleNode.getChildByName("obstacle3").getComponent("Obstacle").getType();
  246. var specialOffsetY = 0
  247. if (type == cfg.obstacleType.circle3) {
  248. if (obstacleType == cfg.obstacleType.square || obstacleType == cfg.obstacleType.circle2) {
  249. specialOffsetY = 100;
  250. }
  251. } else if (type == cfg.obstacleType.circle2) {
  252. if (obstacleType == cfg.obstacleType.square) {
  253. specialOffsetY = 100;
  254. }
  255. }
  256. obstacle1.setPosition(0, self.obstacleNode.getChildByName("obstacle3").y + (cc.winSize.height + cfg.obstacleProperty[type].addY + specialOffsetY) * (1334 / cc.winSize.height) + disObstacleY);
  257. } else {
  258. obstacle1.setPosition(0, 500 * (1334 / cc.winSize.height) + 100);
  259. }
  260. obstacle1.getComponent("Obstacle").initObstacle(obstacleType, self.obstacleNum, self.sumScore);
  261. } else if (self.obstacleNode.getChildByName("obstacle2") == null) {
  262. var obstacle2 = cc.instantiate(prefab);
  263. obstacle2.setName("obstacle2");
  264. self.obstacleNode.addChild(obstacle2);
  265. if (self.obstacleNode.getChildByName("obstacle1") != null) {
  266. var type = self.obstacleNode.getChildByName("obstacle1").getComponent("Obstacle").getType();
  267. var specialOffsetY = 0
  268. if (type == cfg.obstacleType.circle3) {
  269. if (obstacleType == cfg.obstacleType.square || obstacleType == cfg.obstacleType.circle2) {
  270. specialOffsetY = 100;
  271. }
  272. } else if (type == cfg.obstacleType.circle2) {
  273. if (obstacleType == cfg.obstacleType.square) {
  274. specialOffsetY = 100;
  275. }
  276. }
  277. obstacle2.setPosition(0, self.obstacleNode.getChildByName("obstacle1").y + (cc.winSize.height + cfg.obstacleProperty[type].addY + specialOffsetY) * (1334 / cc.winSize.height) + disObstacleY);
  278. } else {
  279. obstacle2.setPosition(0, 500 * (1334 / cc.winSize.height) + 100);
  280. }
  281. obstacle2.getComponent("Obstacle").initObstacle(obstacleType, self.obstacleNum, self.sumScore);
  282. } else if (self.obstacleNode.getChildByName("obstacle3") == null) {
  283. var obstacle3 = cc.instantiate(prefab);
  284. obstacle3.setName("obstacle3");
  285. self.obstacleNode.addChild(obstacle3);
  286. if (self.obstacleNode.getChildByName("obstacle2") != null) {
  287. var type = self.obstacleNode.getChildByName("obstacle2").getComponent("Obstacle").getType();
  288. var specialOffsetY = 0
  289. if (type == cfg.obstacleType.circle3) {
  290. if (obstacleType == cfg.obstacleType.square || obstacleType == cfg.obstacleType.circle2) {
  291. specialOffsetY = 100;
  292. }
  293. } else if (type == cfg.obstacleType.circle2) {
  294. if (obstacleType == cfg.obstacleType.square) {
  295. specialOffsetY = 100;
  296. }
  297. }
  298. obstacle3.setPosition(0, self.obstacleNode.getChildByName("obstacle2").y + (cc.winSize.height + cfg.obstacleProperty[type].addY + specialOffsetY) * (1334 / cc.winSize.height) + disObstacleY);
  299. } else {
  300. obstacle3.setPosition(0, 500 * (1334 / cc.winSize.height) + 100);
  301. }
  302. obstacle3.getComponent("Obstacle").initObstacle(obstacleType, self.obstacleNum, self.sumScore);
  303. }
  304. })
  305. },
  306. randomObstacleType: function () {
  307. var obstacleType = 0;
  308. this.obstacleNum++;
  309. var randNum = Math.floor(Math.random() * 100) + 1;
  310. if (this.obstacleNum == 1) {
  311. obstacleType = 0;
  312. } else if (this.obstacleNum > 1 && this.obstacleNum <= 5) {
  313. if (randNum > 0 && randNum <= 35) {
  314. obstacleType = 0;
  315. } else if (randNum > 35 && randNum <= 70) {
  316. obstacleType = 1;
  317. } else if (randNum > 70 && randNum <= 85) {
  318. obstacleType = 2;
  319. } else if (randNum > 85 && randNum <= 100) {
  320. obstacleType = 3;
  321. }
  322. } else if (this.obstacleNum > 15 && this.obstacleNum <= 45) {
  323. if (randNum > 0 && randNum <= 20) {
  324. obstacleType = 0;
  325. } else if (randNum > 20 && randNum <= 40) {
  326. obstacleType = 1;
  327. } else if (randNum > 40 && randNum <= 60) {
  328. obstacleType = 2;
  329. } else if (randNum > 60 && randNum <= 80) {
  330. obstacleType = 3;
  331. } else if (randNum > 80 && randNum <= 90) {
  332. obstacleType = 4;
  333. } else if (randNum > 90 && randNum <= 100) {
  334. obstacleType = 5;
  335. }
  336. } else if (this.obstacleNum > 45) {
  337. var randNum = Math.floor(Math.random() * 100) + 1;
  338. if (randNum > 0 && randNum <= 10) {
  339. obstacleType = 0;
  340. } else if (randNum > 10 && randNum <= 20) {
  341. obstacleType = 1;
  342. } else if (randNum > 20 && randNum <= 35) {
  343. obstacleType = 2;
  344. } else if (randNum > 35 && randNum <= 50) {
  345. obstacleType = 3;
  346. } else if (randNum > 50 && randNum <= 75) {
  347. obstacleType = 4;
  348. } else if (randNum > 75 && randNum <= 100) {
  349. obstacleType = 5;
  350. }
  351. }
  352. return obstacleType;
  353. },
  354. destroyObstacle: function () {
  355. if (!this.obstacleNode.getChildByName("obstacle1").active) {
  356. this.obstacleNode.removeChild(this.obstacleNode.getChildByName("obstacle1"));
  357. }
  358. if (!this.obstacleNode.getChildByName("obstacle2").active) {
  359. this.obstacleNode.removeChild(this.obstacleNode.getChildByName("obstacle2"));
  360. }
  361. if (!this.obstacleNode.getChildByName("obstacle3").active) {
  362. this.obstacleNode.removeChild(this.obstacleNode.getChildByName("obstacle3"));
  363. }
  364. this.createObstacle();
  365. },
  366. registerTouchEventHandle: function () {
  367. this.node.on(cc.Node.EventType.TOUCH_START, function (event) {
  368. if (!this.bDead && !this.bPause) {
  369. this.buDing.getComponent("BuDingSprite").jump();
  370. }
  371. }, this)
  372. this.node.on(cc.Node.EventType.TOUCH_END, function (event) {
  373. }, this)
  374. },
  375. addScore: function () {
  376. cc.audioEngine.playEffect(this.eatGoldAudio);
  377. var addScoreLabel = this.node.getChildByName("AddScoreLabel");
  378. addScoreLabel.getComponent("cc.Label").string = "/" + 1;
  379. addScoreLabel.active = true;
  380. addScoreLabel.opacity = 255;
  381. addScoreLabel.runAction(cc.fadeTo(1, 0));
  382. this.sumScore += 1;
  383. this.starNum++;
  384. var sumScoreLabel = this.scoreNode.getChildByName("ScoreLabel");
  385. sumScoreLabel.getComponent("cc.Label").string = this.sumScore;
  386. sumScoreLabel.runAction(cc.sequence(cc.scaleTo(0.5, 1.2), cc.scaleTo(0.5, 1)));
  387. //超越
  388. // if (UserInfo.platform == 1 && cc.vv.isRank) {
  389. // this.node.getChildByName("Pass").active = true;
  390. // wx.postMessage({
  391. // method: 'checkPass',
  392. // data: this.sumScore
  393. // });
  394. // }
  395. // if (this.starNum >= 2)
  396. // {
  397. // var isVisible = (this.starNum%2!=0);
  398. // this.obstacleNode.getChildByName("obstacle1").active = isVisible;
  399. // this.obstacleNode.getChildByName("obstacle2").active = !isVisible;
  400. // this.destroyObstacle();
  401. // }
  402. },
  403. showPass: function () {
  404. if (UserInfo.platform == 1 && cc.vv.isRank) {
  405. let openDataContext = wx.getOpenDataContext();
  406. let sharedCanvas = openDataContext.canvas;
  407. this.childCanvas.initWithElement(sharedCanvas);
  408. this.childCanvas.handleLoadedTexture();
  409. this.passNode.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(this.childCanvas);
  410. //var scale = Math.min(750 / this.feiendtex.width, 1334 / this.feiendtex.height);
  411. //this.passNode.setContentSize(this.feiendtex.width * scale, this.feiendtex.height * scale);
  412. }
  413. },
  414. addLayerNum: function () {
  415. cc.audioEngine.playEffect(this.changeColorAudio);
  416. //var layerNumLabel = this.scoreNode.getChildByName("LayerLabel");
  417. this.layerNum++;
  418. //layerNumLabel.getComponent("cc.Label").string = this.layerNum;
  419. //layerNumLabel.runAction(cc.sequence(cc.scaleTo(0.5,1.2),cc.scaleTo(0.5,1)));
  420. //this.buDing.getComponent("BuDingSprite").changeColor(colorType);
  421. if (this.layerNum >= 2) {
  422. if (this.layerNum % 3 == 0) {
  423. this.obstacleNode.getChildByName("obstacle1").active = true;
  424. this.obstacleNode.getChildByName("obstacle2").active = false;
  425. this.obstacleNode.getChildByName("obstacle3").active = true;
  426. } else if (this.layerNum % 3 == 1) {
  427. this.obstacleNode.getChildByName("obstacle1").active = true;
  428. this.obstacleNode.getChildByName("obstacle2").active = true;
  429. this.obstacleNode.getChildByName("obstacle3").active = false;
  430. } else if (this.layerNum % 3 == 2) {
  431. this.obstacleNode.getChildByName("obstacle1").active = false;
  432. this.obstacleNode.getChildByName("obstacle2").active = true;
  433. this.obstacleNode.getChildByName("obstacle3").active = true;
  434. }
  435. this.destroyObstacle();
  436. }
  437. },
  438. onPauseBtn: function () {
  439. this.bPause = true;
  440. this.buDing.getComponent("BuDingSprite").stopBuding();
  441. this.node.getChildByName("Mask").active = true;
  442. this.funcNode.getChildByName("PauseBtn").active = false;
  443. this.funcNode.getChildByName("PlayBtn").active = true;
  444. var obstacle1 = this.obstacleNode.getChildByName("obstacle1");
  445. var obstacle2 = this.obstacleNode.getChildByName("obstacle2");
  446. var obstacle3 = this.obstacleNode.getChildByName("obstacle3");
  447. if (obstacle1 != null) {
  448. obstacle1.getComponent("Obstacle").pauseObstacleAction();
  449. }
  450. if (obstacle2 != null) {
  451. obstacle2.getComponent("Obstacle").pauseObstacleAction();
  452. }
  453. if (obstacle3 != null) {
  454. obstacle3.getComponent("Obstacle").pauseObstacleAction();
  455. }
  456. },
  457. onPlayBtn: function () {
  458. var playBtn = this.funcNode.getChildByName("PlayBtn");
  459. playBtn.getComponent("cc.Button").interactable = false;
  460. playBtn.getChildByName("PlaySprite").active = false;
  461. var countDownLabel = this.funcNode.getChildByName("CountDownLabel");
  462. countDownLabel.active = true;
  463. var countDown = 3;
  464. countDownLabel.getComponent("cc.Label").string = countDown;
  465. var mask = this.node.getChildByName("Mask");
  466. var pauseBtn = this.funcNode.getChildByName("PauseBtn");
  467. var obstacle1 = this.obstacleNode.getChildByName("obstacle1");
  468. var obstacle2 = this.obstacleNode.getChildByName("obstacle2");
  469. var obstacle3 = this.obstacleNode.getChildByName("obstacle3");
  470. var self = this;
  471. var callback = function () {
  472. if (countDown <= 1) {
  473. playBtn.active = false;
  474. playBtn.getComponent("cc.Button").interactable = true;
  475. playBtn.getChildByName("PlaySprite").active = true;
  476. countDownLabel.active = false;
  477. var callback1 = function () {
  478. mask.active = false;
  479. pauseBtn.active = true;
  480. self.bPause = false;
  481. self.buDing.getComponent("BuDingSprite").resumeBuding();
  482. if (obstacle1 != null) {
  483. obstacle1.getComponent("Obstacle").resumeObstacleAction();
  484. }
  485. if (obstacle2 != null) {
  486. obstacle2.getComponent("Obstacle").resumeObstacleAction();
  487. }
  488. if (obstacle3 != null) {
  489. obstacle3.getComponent("Obstacle").resumeObstacleAction();
  490. }
  491. }
  492. self.funcNode.runAction(cc.sequence(cc.delayTime(0.5), cc.callFunc(callback1, this)));
  493. } else {
  494. countDown--;
  495. countDownLabel.getComponent("cc.Label").string = countDown;
  496. countDownLabel.runAction(cc.sequence(cc.delayTime(1), cc.callFunc(callback, this)));
  497. }
  498. }
  499. countDownLabel.runAction(cc.sequence(cc.delayTime(1), cc.callFunc(callback, this)));
  500. },
  501. onChallengeBtn: function () {
  502. var sharedesc = "";
  503. sharedesc = '我的布丁穿过了' + this.sumScore + '个障碍物,快来挑战我吧!';
  504. var data = {
  505. titleData: sharedesc,
  506. imgurlData: UserInfo.shareUrl,
  507. queryData: 'pp=pp'
  508. }
  509. WxCommon.ShareAppMessage(data, this, null);
  510. },
  511. onShareBtn: function () {
  512. //接分享
  513. if (CC_WECHATGAME && cc.vv.isRank) {
  514. var data = {
  515. titleData: UserInfo.shareDesc,
  516. imgurlData: UserInfo.shareUrl,
  517. queryData: 'pp=pp'
  518. }
  519. this.shareFlag = 1;
  520. WxCommon.ShareAppMessage(data, this.node.getComponent("GameSence"), this.node.getComponent("GameSence").comeBack);
  521. wx.onShow(res => {
  522. if(this.shareFlag == 1) {
  523. var gameOverNode = this.node.getChildByName("GameOver");
  524. var countDownLabel = gameOverNode.getChildByName("ShareNode").getChildByName("EndCountDownLabel");
  525. countDownLabel.stopAllActions();
  526. this.continueGame();
  527. cc.audioEngine.playMusic(this.gamebgMusic, true);
  528. this.shareFlag = 0;
  529. }
  530. })
  531. } else {
  532. var gameOverNode = this.node.getChildByName("GameOver");
  533. var countDownLabel = gameOverNode.getChildByName("ShareNode").getChildByName("EndCountDownLabel");
  534. countDownLabel.stopAllActions();
  535. this.continueGame();
  536. cc.audioEngine.playMusic(this.gamebgMusic, true);
  537. }
  538. },
  539. onWatchVideoBtn: function () {
  540. var gameOverNode = this.node.getChildByName("GameOver");
  541. var countDownLabel = gameOverNode.getChildByName("ShareNode").getChildByName("EndCountDownLabel");
  542. if (CC_WECHATGAME && window.wx.createRewardedVideoAd) {
  543. this.bShare = false;
  544. countDownLabel.stopAllActions();
  545. if(this.debugErrorTips) {
  546. this.showWatchVdError();
  547. return;
  548. }
  549. let videoAd = wx.createRewardedVideoAd({
  550. adUnitId: 'adunit-563ed529855c1195'
  551. })
  552. videoAd.onClose(res => {
  553. // 用户点击了【关闭广告】按钮
  554. // 小于 2.1.0 的基础库版本,res 是一个 undefined
  555. if (res && res.isEnded || res === undefined) {
  556. // 正常播放结束,可以下发游戏奖励
  557. this.continueGame();
  558. cc.audioEngine.playMusic(this.gamebgMusic, true);
  559. } else {
  560. // 播放中途退出,不下发游戏奖励
  561. this.startFinishCount();
  562. }
  563. })
  564. videoAd.onError(res => {
  565. this.showWatchVdError()
  566. })
  567. videoAd.load()
  568. .then(() => videoAd.show())
  569. .catch(err => {
  570. videoAd.load()
  571. .then(() => videoAd.show())
  572. })
  573. } else {
  574. }
  575. },
  576. showWatchVdError: function() {
  577. if(CC_WECHATGAME) {
  578. wx.showModal({
  579. title: "提示",
  580. content: "拉取视频广告失败,邀请好友一起通关可获得一次复活机会",
  581. showCancel: false,
  582. cancelText: "取消",
  583. confirmText: "确定",
  584. success: () => {
  585. var gameOverNode = this.node.getChildByName("GameOver");
  586. var shareNode = gameOverNode.getChildByName("ShareNode");
  587. //显示邀请好友按钮
  588. shareNode.getChildByName("ShareBtn").active = true;
  589. shareNode.getChildByName("VideoBtn").active = false;
  590. this.startFinishCount();
  591. }
  592. })
  593. }
  594. },
  595. //分享到群复活
  596. comeBack: function (shareTickets) {
  597. //console.log("我又回来啦");
  598. if (!UserInfo.IsEmpty(shareTickets)) {
  599. let currentShareCount = parseInt(UserInfo.getString("shareWXCount"));
  600. if (currentShareCount > 0) {
  601. currentShareCount--;
  602. UserInfo.setString("shareWXCount", currentShareCount);
  603. var gameOverNode = this.node.getChildByName("GameOver");
  604. var countDownLabel = gameOverNode.getChildByName("ShareNode").getChildByName("EndCountDownLabel");
  605. countDownLabel.stopAllActions();
  606. this.continueGame();
  607. cc.audioEngine.playMusic(this.gamebgMusic, true);
  608. if (UserInfo.platform == 1 && cc.vv.isRank) {
  609. wx.postMessage({
  610. method: 'hideRank',
  611. data: "false"
  612. });
  613. cc.find("Canvas/PHB").active = false;
  614. }
  615. } else {
  616. UserInfo.setString("shareWXCount", 0);
  617. }
  618. }
  619. },
  620. onPassBtn: function () {
  621. var gameOverNode = this.node.getChildByName("GameOver");
  622. var countDownLabel = gameOverNode.getChildByName("ShareNode").getChildByName("EndCountDownLabel");
  623. countDownLabel.stopAllActions();
  624. gameOverNode.getChildByName("ShareNode").active = false;
  625. gameOverNode.getChildByName("EndNode").active = true;
  626. this.gameOver();
  627. var data = [{
  628. key: UserInfo.getCurWeekScoreValue(),
  629. value: UserInfo.getScore().toString()
  630. },
  631. {
  632. key: "driLevel",
  633. value: '0'
  634. }
  635. ];
  636. if (CC_WECHATGAME && cc.vv.isRank) {
  637. this.showRank();
  638. this.updateXYXScore('showMyRank', data);
  639. }
  640. },
  641. onRestartBtn: function () {
  642. this.node.getChildByName("GameOver").active = false;
  643. this.node.parent.getChildByName("PHB").active = false;
  644. this.startGame();
  645. },
  646. onShopBtn: function () {
  647. },
  648. onRankBtn: function (event, optData) {
  649. if (CC_WECHATGAME && cc.vv.isRank) {
  650. wx.postMessage({
  651. method: 'showFriendRank',
  652. data: optData
  653. });
  654. this.node.parent.getChildByName("PHB").getChildByName("BtnPHBClose").active = (optData == "true");
  655. this.node.parent.getChildByName("PHB").getChildByName("emptyBtn").active = (optData == "true");
  656. this.node.parent.getChildByName("PHB").getChildByName("bg").active = (optData == "true");
  657. if ("true" == optData) {
  658. this.showRank();
  659. }
  660. if ("false" == optData) {
  661. wx.postMessage({
  662. method: 'hideRank',
  663. data: optData
  664. });
  665. cc.find("Canvas/PHB").active = false;
  666. }
  667. }
  668. },
  669. updateXYXScore(strFun, data) {
  670. data.push({
  671. key: "starrank",
  672. value: UserInfo.getScore().toString()
  673. })
  674. wx.setUserCloudStorage({
  675. KVDataList: data,
  676. success: function (res) {
  677. wx.postMessage({
  678. method: strFun
  679. });
  680. },
  681. fail: function (res) {
  682. console.error(res);
  683. },
  684. complete: function (res) {}
  685. })
  686. },
  687. showRank() {
  688. if (UserInfo.platform == 1 && cc.vv.isRank) {
  689. let openDataContext = wx.getOpenDataContext();
  690. let sharedCanvas = openDataContext.canvas;
  691. this.feiendtex.initWithElement(sharedCanvas);
  692. this.feiendtex.handleLoadedTexture();
  693. cc.find("Canvas/PHB").getChildByName("feiendrank").getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(this.feiendtex);
  694. //cc.find("Canvas/PHB").getChildByName("feiendrank").setContentSize(720,1280);
  695. var scale = Math.min(750 / this.feiendtex.width, 1334 / this.feiendtex.height);
  696. cc.find("Canvas/PHB").getChildByName("feiendrank").setContentSize(this.feiendtex.width * scale, this.feiendtex.height * scale);
  697. cc.find("Canvas/PHB").active = true;
  698. this.node.getChildByName("Pass").active = false;
  699. }
  700. },
  701. onHonourBtn: function () {
  702. },
  703. onMoreGameBtn: function (optData) {
  704. var endNode = this.node.getChildByName("GameOver").getChildByName("EndNode");
  705. endNode.getChildByName("ChallengeBtn").getComponent("cc.Button").interactable = (optData == "false");
  706. endNode.getChildByName("RestartBtn").getComponent("cc.Button").interactable = (optData == "false");
  707. endNode.getChildByName("SkinBtn").getComponent("cc.Button").interactable = (optData == "false");
  708. endNode.getChildByName("RankBtn").getComponent("cc.Button").interactable = (optData == "false");
  709. endNode.getChildByName("MoreGameBtn").getComponent("cc.Button").interactable = (optData == "false");
  710. endNode.getChildByName("HonourBtn").getComponent("cc.Button").interactable = (optData == "false");
  711. },
  712. onBackHomeBtn: function() {
  713. this.node.getChildByName("GameOver").active = false;
  714. this.node.parent.getChildByName("PHB").active = false;
  715. this.node.parent.getComponent("SenceManager").enterSence("Game", "Home");
  716. },
  717. onBtnAudio: function () {
  718. cc.audioEngine.playEffect(this.buttonAudio);
  719. },
  720. update: function (dt) {
  721. if (cc.find("Canvas/PHB").active) {
  722. this.showRank();
  723. //console.log("显示排行榜");
  724. }
  725. // if (this.node.getChildByName("Pass").active)
  726. // {
  727. // this.showPass();
  728. // }
  729. },
  730. });