GameSence.js 37 KB

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