GameSence.js 38 KB

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