MainScene.ts 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. import Global from "./Global"
  2. import Http from "./Http";
  3. const { ccclass, property } = cc._decorator;
  4. @ccclass
  5. export default class MainScene extends cc.Component {
  6. @property(cc.Prefab)
  7. boli: cc.Prefab = null;
  8. @property(cc.Prefab)
  9. dici: cc.Prefab = null;
  10. @property(cc.Prefab)
  11. lvdai: cc.Prefab = null;
  12. @property(cc.Prefab)
  13. shandian: cc.Prefab = null;
  14. @property(cc.Prefab)
  15. tanhuang: cc.Prefab = null;
  16. @property(cc.Prefab)
  17. Opplvdai: cc.Prefab = null;
  18. @property(cc.Prefab)
  19. GD: cc.Prefab = null;
  20. @property(cc.Prefab)
  21. failure: cc.Prefab = null;
  22. @property(cc.Prefab)
  23. fuhuo: cc.Prefab = null;
  24. @property(cc.Node)
  25. Player: cc.Node = null;
  26. @property(cc.Node)
  27. FHolderNode: cc.Node = null;
  28. @property(cc.Node)
  29. Bg: cc.Node = null;
  30. @property(cc.Node)
  31. LifeDing: cc.Node = null;
  32. @property(cc.Button)
  33. LEFT: cc.Button = null;
  34. @property(cc.Button)
  35. RIGHT: cc.Button = null;
  36. @property(cc.Label)
  37. output: cc.Label = null;
  38. // LIFE-CYCLE CALLBACKS:
  39. /**
  40. * 上一个落脚点生成时间
  41. */
  42. private STime = 0;
  43. /**
  44. * 当前落脚点生成时间
  45. */
  46. private ETime = 0;
  47. /** Left*/
  48. private LkeyDown = false;
  49. /** Right*/
  50. private RkeyDown = false;
  51. /**the loght FootHolder F */
  52. private lightF = false;
  53. moveFalg = "";
  54. USERINFO = {
  55. userId: "",
  56. score: 0,
  57. }
  58. onLoad() {
  59. var time = cc.sys.localStorage.getItem("hfsj");
  60. if (time == null || time == undefined || time == "") {
  61. cc.sys.localStorage.setItem("hfsj", "99");
  62. }
  63. this.LifeDing.zIndex = 10;
  64. this.FHolderNode.zIndex = 9;
  65. Global.instance.setMN(this.node);
  66. this.STime = Date.now();
  67. let FHolder = cc.instantiate(this.GD);
  68. this.FHolderNode.addChild(FHolder, 10, "GD");
  69. FHolder.getComponent("GD").init(this, 1);
  70. FHolder.y = -150;
  71. let FHolder2 = cc.instantiate(this.lvdai);
  72. this.FHolderNode.addChild(FHolder2, 10, "lvdai");
  73. FHolder2.getComponent("lvdai").init(this);
  74. FHolder2.y = -300;
  75. // let FHolder2 = cc.instantiate(this.tanhuang);
  76. // this.FHolderNode.addChild(FHolder2,10,"tanhuang");
  77. // FHolder2.getComponent("tanhuang").init(this);
  78. // FHolder2.y = -300;
  79. let FHolder3 = cc.instantiate(this.GD);
  80. this.FHolderNode.addChild(FHolder3, 10, "GD");
  81. FHolder3.getComponent("GD").init(this, 1);
  82. FHolder3.y = -450;
  83. // this.Player.active = false;
  84. this.Player.x = FHolder.x;
  85. // this.Player.y = FHolder.y+60;
  86. this.Player.zIndex = 11;
  87. for (let i = 0; i < this.LifeDing.children.length; i++) {
  88. if (this.LifeDing.children[i].name == "lifeBG") {
  89. if (!this.LifeDing.children[i].active) {
  90. this.LifeDing.children[i].active = true;
  91. }
  92. Global.instance.reLife.push(this.LifeDing.children[i]);
  93. }
  94. }
  95. cc.director.getCollisionManager().enabled = true;
  96. // cc.director.getCollisionManager().enabledDebugDraw = true;
  97. cc.director.getCollisionManager().enabledDrawBoundingBox = false;
  98. }
  99. playtansound() {
  100. if (Global.instance.OverFlag == false) {
  101. cc.loader.loadRes("tan", cc.AudioClip, function (err, clip) {
  102. var audioID = cc.audioEngine.play(clip, false, 0.5);
  103. });
  104. }
  105. }
  106. playdeadsound() {
  107. cc.loader.loadRes("dead", cc.AudioClip, function (err, clip) {
  108. var audioID = cc.audioEngine.play(clip, false, 0.5);
  109. });
  110. }
  111. start() {
  112. }
  113. update(dt) {
  114. // console.log(Global.instance.CollisionFlag);
  115. this.MoveBg();
  116. Global.instance.moveSpeed = 1;
  117. let FHolder;
  118. if ((this.ETime - this.STime) > (1200 - (180 * (Global.instance.FHFallSpeed - 2)))) {//控制落脚点之间的间距,间距144px
  119. this.STime = Date.now();
  120. if (!Global.instance.OverFlag) {
  121. FHolder = this.FootHoldGenerator(null);
  122. // this.Score();
  123. }
  124. }
  125. this.FHolder();
  126. this.reduceLife();
  127. if (Global.instance.CollisionFlag) {//左右传送带减速
  128. switch (Global.instance.KIND_FootHold) {
  129. case 3: {
  130. this.Player.x += 2;
  131. break;
  132. }
  133. case 4: {
  134. this.Player.x -= 2;
  135. break;
  136. }
  137. case 7: {
  138. if (Global.instance.LorR == 1) {
  139. this.Player.x--;
  140. }
  141. else {
  142. this.Player.x++;
  143. }
  144. break;
  145. }
  146. default: {
  147. break;
  148. }
  149. }
  150. }
  151. if (this.Player.x < -180) {
  152. this.Player.x = -180;
  153. }
  154. if (this.Player.x > 180) {
  155. this.Player.x = 180;
  156. }
  157. if (this.Player.y < (-510)) {
  158. for (let i = 0; i < this.LifeDing.children.length; i++) {
  159. if (this.LifeDing.children[i].name == "lifeBG") {
  160. this.LifeDing.children[i].active = false;
  161. }
  162. }
  163. this.gameOver();
  164. }
  165. if (Global.instance.reLife.length == 0) {
  166. this.gameOver();
  167. }
  168. this.ETime = Date.now();
  169. let AllChildren = new Array();
  170. let GDChildren = new Array();
  171. AllChildren = this.node.getChildByName("BgNode").getChildByName("FHolder").children;
  172. for (let i = 0; i < AllChildren.length; i++) {
  173. if (AllChildren[i].name == "GD") {
  174. GDChildren.push(AllChildren[i]);
  175. }
  176. }
  177. // cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN,this.onKeyDown,this);
  178. //cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP,this.onKeyUp,this);
  179. this.Listener();
  180. }
  181. Listener() {
  182. let btnClickArr = new Array(2);
  183. if (!this.LkeyDown) {
  184. this.RIGHT.node.on(cc.Node.EventType.TOUCH_START, this.BtnTurnRight, this);
  185. this.RIGHT.node.on(cc.Node.EventType.TOUCH_MOVE, this.BtnTurnRight, this);
  186. this.RIGHT.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onKeyUp, this);
  187. btnClickArr.push(this.RIGHT);
  188. this.RIGHT.node.on(cc.Node.EventType.TOUCH_END, () => {
  189. this.onKeyUp;
  190. }, this);
  191. }
  192. else {
  193. this.RIGHT.node.off(cc.Node.EventType.TOUCH_START, this.BtnTurnRight, this);
  194. this.RIGHT.node.off(cc.Node.EventType.TOUCH_MOVE, this.BtnTurnRight, this);
  195. this.RIGHT.node.off(cc.Node.EventType.TOUCH_CANCEL, this.onKeyUp, this);
  196. this.RIGHT.node.off(cc.Node.EventType.TOUCH_END, () => {
  197. this.onKeyUp;
  198. }, this);
  199. }
  200. if (!this.RkeyDown) {
  201. this.LEFT.node.on(cc.Node.EventType.TOUCH_START, this.BtnTurnLeft, this);
  202. this.LEFT.node.on(cc.Node.EventType.TOUCH_MOVE, this.BtnTurnLeft, this);
  203. this.LEFT.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onKeyUp, this);
  204. // btnClickArr.push(this.LEFT);
  205. this.LEFT.node.on(cc.Node.EventType.TOUCH_END, () => {
  206. this.onKeyUp;
  207. }, this);
  208. }
  209. else {
  210. this.LEFT.node.off(cc.Node.EventType.TOUCH_START, this.BtnTurnLeft, this);
  211. this.LEFT.node.off(cc.Node.EventType.TOUCH_MOVE, this.BtnTurnLeft, this);
  212. this.LEFT.node.off(cc.Node.EventType.TOUCH_CANCEL, this.onKeyUp, this);
  213. this.LEFT.node.off(cc.Node.EventType.TOUCH_END, () => {
  214. this.onKeyUp;
  215. }, this);
  216. }
  217. }
  218. StopAni(self) {
  219. if (!Global.instance.AniFalg) {
  220. let FHolder = self.node.getChildByName("BgNode").getChildByName("FHolder").children;
  221. let Fname;
  222. for (let i = 0; FHolder.length; i++) {
  223. if (i == FHolder.length - 1) {
  224. Global.instance.AniFalg = true;
  225. return;
  226. }
  227. Fname = FHolder[i].name;
  228. FHolder[i].getComponent(Fname).enabled = false;
  229. }
  230. }
  231. }
  232. /**
  233. * player 移出落脚点
  234. */
  235. FHolder() {
  236. let self = this
  237. let FHArray = this.FHolderNode.children;
  238. let Ani;//the Animation of Player when player fall down
  239. let Anistate;//the state of Ani;
  240. for (let i = FHArray.length - 1; i >= 0; i--) {
  241. if (FHArray[i].isHold) {
  242. // let nameNode:string = FHArray[i].name;
  243. if (self.Player.x > (FHArray[i].x + 75)) {
  244. Global.instance.CollisionFlag = false;
  245. FHArray[i].isHold = false;
  246. // this.Score();
  247. }
  248. else if (self.Player.x < (FHArray[i].x - 75)) {
  249. Global.instance.CollisionFlag = false;
  250. FHArray[i].isHold = false;
  251. // this.Score();
  252. }
  253. else {
  254. Global.instance.CollisionFlag = true;
  255. FHArray[i].isHold = true;
  256. }
  257. }
  258. }
  259. }
  260. /**
  261. * 移动背景,初始移动速度200px/s
  262. */
  263. MoveBg() {
  264. let Bg0 = this.Bg.getChildByName("Bg_0");
  265. let Bg1 = this.Bg.getChildByName("Bg_1");
  266. if (Global.instance.OverFlag) {
  267. }
  268. else {
  269. Bg0.y += Global.instance.FHFallSpeed + 1;
  270. Bg1.y += Global.instance.FHFallSpeed + 1;
  271. if (Bg0.y > 946) {
  272. Bg0.y = -946;
  273. }
  274. if (Bg1.y > 946) {
  275. Bg1.y = -946;
  276. }
  277. }
  278. }
  279. /**
  280. * 生成落脚点
  281. */
  282. FootHoldGenerator(rtpe) {
  283. let self = this;
  284. let Magnification = 0;
  285. let f = parseInt(self.LifeDing.getChildByName("Floor").getComponent(cc.Label).string);
  286. if (f < 20) {
  287. Magnification = 2
  288. }
  289. else if (f < 40) {
  290. Magnification = 4;
  291. }
  292. else if (f < 60) {
  293. Magnification = 6;
  294. }
  295. else if (f < 80) {
  296. Magnification = 8;
  297. }
  298. else {
  299. Magnification = 8;
  300. }
  301. let KindHolder = Math.ceil(Math.random() * Magnification);
  302. // KindHolder = 7;
  303. let FHolder;
  304. // KindHolder = Math.ceil(Math.random()*7);
  305. if (KindHolder == 3 || KindHolder == 4) {
  306. let va = Math.random() * 10;
  307. if (va >= 1 && va < 4) {
  308. KindHolder = 7;
  309. } else if (va >= 4 && va < 10) {
  310. KindHolder = 5;
  311. }
  312. //KindHolder = 5;
  313. console.log("KindHolder gb: " + KindHolder);
  314. }
  315. if (rtpe != null) {
  316. KindHolder = rtpe;
  317. console.log("KindHolder gb2: " + KindHolder);
  318. }
  319. this.ETime = Date.now();
  320. switch (KindHolder) {
  321. case 1: {
  322. FHolder = cc.instantiate(self.GD);
  323. self.FHolderNode.addChild(FHolder, 5, "GD");
  324. FHolder.getComponent("GD").init(self, 1);
  325. FHolder.isHold = false;
  326. // console.log("产生第一种落脚点");
  327. break;
  328. }
  329. case 2: {
  330. FHolder = cc.instantiate(self.GD);
  331. self.FHolderNode.addChild(FHolder, 5, "GD");
  332. FHolder.getComponent("GD").init(self, 7);
  333. FHolder.isHold = false;
  334. /*self.FootHoldGenerator();
  335. FHolder = cc.instantiate(self.tanhuang);
  336. self.FHolderNode.addChild(FHolder,5,"tanhuang");
  337. FHolder.getComponent("tanhuang").init(self);
  338. FHolder.isHold = false;
  339. // // console.log("产生第五种落脚点");
  340. // break;*/
  341. break;
  342. }
  343. case 3: {
  344. FHolder = cc.instantiate(self.lvdai);
  345. self.FHolderNode.addChild(FHolder, 5, "lvdai");
  346. FHolder.getComponent("lvdai").init(self);
  347. FHolder.isHold = false;
  348. // console.log("产生第二种落脚点");
  349. console.log(" 创建滚动 ");
  350. break;
  351. }
  352. case 4: {
  353. FHolder = cc.instantiate(self.lvdai);
  354. self.FHolderNode.addChild(FHolder, 5, "lvdai");
  355. FHolder.getComponent("lvdai").init(self);
  356. FHolder.isHold = false;
  357. // console.log("产生第五种落脚点");
  358. console.log(" 创建滚动1 ");
  359. break;
  360. }
  361. case 5: {
  362. FHolder = cc.instantiate(self.boli);
  363. self.FHolderNode.addChild(FHolder, 5, "boli");
  364. FHolder.getComponent("boli").init(self);
  365. FHolder.isHold = false;
  366. // console.log("产生第三种落脚点");
  367. break;
  368. }
  369. case 6: {
  370. FHolder = cc.instantiate(self.dici);
  371. self.FHolderNode.addChild(FHolder, 5, "dici");
  372. FHolder.getComponent("dici").init(self);
  373. FHolder.isHold = false;
  374. // console.log("产生第四种落脚点");
  375. break;
  376. }
  377. case 7: {
  378. FHolder = cc.instantiate(self.GD);
  379. self.FHolderNode.addChild(FHolder, 5, "GD");
  380. FHolder.getComponent("GD").init(self, 7);
  381. FHolder.isHold = false;
  382. break;
  383. }
  384. case 8: {
  385. if (self.lightF) {
  386. self.lightF = false;
  387. self.FootHoldGenerator(null);
  388. break;
  389. }
  390. FHolder = cc.instantiate(self.shandian);
  391. self.FHolderNode.addChild(FHolder, 5, "shandian");
  392. FHolder.getComponent("shandian").init(self);
  393. FHolder.isHold = false;
  394. self.lightF = true;
  395. break;
  396. }
  397. // default:{
  398. // FHolder = cc.instantiate(self.GD);
  399. // self.FHolderNode.addChild(FHolder,5,"GD");
  400. // FHolder.getComponent("GD").init(self);
  401. // FHolder.getComponent("GD").enabled = true;
  402. // FHolder.getComponent("MoveGD").init(self);
  403. // FHolder.getComponent("MoveGD").enabled = false;
  404. // FHolder.isHold = false;
  405. // // console.log("默认产生第一种落脚点");
  406. // }
  407. // break;
  408. }
  409. return FHolder;
  410. }
  411. /**
  412. * 按钮触发,向左,长按事件
  413. */
  414. BtnTurnLeft(event) {
  415. let self = this;
  416. if (self.RkeyDown) {
  417. return;
  418. }
  419. else {
  420. self.LkeyDown = true;
  421. self.RkeyDown = false;
  422. this.BtnLorR(event);
  423. }
  424. }
  425. /**
  426. * 按钮触发,向右,长按事件,长按响应未解决
  427. */
  428. BtnTurnRight(event) {
  429. let self = this;
  430. if (self.LkeyDown) {
  431. return;
  432. }
  433. else {
  434. self.LkeyDown = false
  435. self.RkeyDown = true;
  436. this.BtnLorR(event);
  437. }
  438. }
  439. /**
  440. * btn触发事件
  441. * @param event 按钮触发事件
  442. */
  443. BtnLorR(event) {
  444. let self = this;
  445. Global.instance.moveSpeed = 1;
  446. let schedule = cc.director.getScheduler();
  447. let stand = self.Player.getChildByName("stand");
  448. let runRight = self.Player.getChildByName("runRight");
  449. let run = self.Player.getChildByName("run");
  450. let Ani;//the animation of player
  451. let Anistate;//the Ani's state
  452. let Anistring;//the name of Ani
  453. let moveByTime = 1;
  454. let scheduleState: boolean = false;//the schedule's state
  455. let schedulePause: boolean = true;
  456. let target: cc.Button = null;//the target which is binged to schedule
  457. let moveByDes = Global.instance.moveSpeed * 120 + 40;
  458. if (self.LkeyDown) {
  459. target = self.LEFT;
  460. moveByTime = 1;
  461. moveByDes = -moveByDes;
  462. scheduleState = schedule.isScheduled(func, target);
  463. // self.output.getComponent(cc.Label).string = Anistring;
  464. schedulePause = schedule.isTargetPaused(target);
  465. Ani = run.getComponent(cc.Animation);
  466. Anistring = "run";
  467. stand.active = false;
  468. runRight.active = false;
  469. run.active = true;
  470. // self.output.getComponent(cc.Label).string = Anistring;
  471. if (!Global.instance.CollisionFlag) {
  472. moveByTime = 1;
  473. }
  474. else {
  475. switch (Global.instance.KIND_FootHold) {
  476. case 2: {
  477. moveByTime = 4.1;
  478. self.output.getComponent(cc.Label).string = Global.instance.KIND_FootHold + "2";
  479. break;
  480. }
  481. case 5: {
  482. moveByTime = 3.9;
  483. // self.output.getComponent(cc.Label).string = Global.instance.KIND_FootHold+"5";
  484. break;
  485. }
  486. };
  487. }
  488. }
  489. if (self.RkeyDown) {
  490. target = self.RIGHT;
  491. moveByTime = 1;
  492. moveByDes = moveByDes;
  493. scheduleState = schedule.isScheduled(func, target);
  494. // self.output.getComponent(cc.Label).string = Anistring;
  495. schedulePause = schedule.isTargetPaused(target);
  496. Ani = runRight.getComponent(cc.Animation);
  497. Anistring = "runR";
  498. stand.active = false;
  499. runRight.active = true;
  500. run.active = false;
  501. // self.output.getComponent(cc.Label).string = Anistring;
  502. if (!Global.instance.CollisionFlag) {
  503. moveByTime = 1;
  504. }
  505. else {
  506. switch (Global.instance.KIND_FootHold) {
  507. case 2: {
  508. moveByTime = 4.1;
  509. // self.output.getComponent(cc.Label).string = Global.instance.KIND_FootHold+"2";
  510. break;
  511. }
  512. case 5: {
  513. moveByTime = 3.9;
  514. // self.output.getComponent(cc.Label).string = Global.instance.KIND_FootHold+"5";
  515. break;
  516. }
  517. };
  518. }
  519. }
  520. let funcFlag = false;
  521. switch (event.type) {
  522. case "touchstart": {
  523. if (!(self.moveFalg.length == 0)) {
  524. break;
  525. }
  526. self.moveFalg = "touchstart";
  527. if (!scheduleState) {
  528. // self.output.getComponent(cc.Label).string = movefalg;
  529. schedule.schedule(func, target, 0);
  530. };
  531. if (schedulePause) {
  532. schedule.resumeTarget(target);
  533. };
  534. break;
  535. }
  536. case "touchmove": {
  537. if (!(self.moveFalg.length == 0)) {
  538. break;
  539. }
  540. self.moveFalg = "touchmove";
  541. // self.output.getComponent(cc.Label).string = movefalg;
  542. if (!scheduleState) {
  543. schedule.schedule(func, target, 0);
  544. };
  545. if (schedulePause) {
  546. schedule.resumeTarget(target);
  547. };
  548. break;
  549. }
  550. case "touchcancel": {
  551. break;
  552. }
  553. case "touchend": {
  554. // self.output.getComponent(cc.Label).string = "touchend";
  555. schedule.pauseTarget(target);
  556. self.Player.stopAllActions();
  557. Ani.stop(Anistring);
  558. moveByTime = 1;
  559. moveByDes = Global.instance.moveSpeed * 120 + 40;
  560. switch (Anistring) {
  561. case "run": {
  562. run.active = false;
  563. runRight.active = false;
  564. stand.active = true;
  565. self.LkeyDown = false;
  566. self.RkeyDown = false;
  567. moveByDes = Global.instance.moveSpeed / 2;
  568. break;
  569. }
  570. case "runR": {
  571. run.active = false;
  572. runRight.active = false;
  573. stand.active = true;
  574. self.LkeyDown = false;
  575. self.RkeyDown = false;
  576. moveByDes = Global.instance.moveSpeed / 2;
  577. break;
  578. }
  579. }
  580. funcFlag = false;
  581. self.moveFalg = "";
  582. break;
  583. }
  584. }
  585. function func() {
  586. if (funcFlag) {
  587. return;
  588. }
  589. if (Global.instance.CollisionFlag) {
  590. moveByTime = 1;
  591. }
  592. let spawn = cc.spawn(cc.callFunc(function () {
  593. self.Player.runAction(cc.moveBy(moveByTime, moveByDes, 0));
  594. }), cc.callFunc(function () {
  595. Anistate = Ani.play(Anistring);
  596. Anistate.speed = 1;
  597. Anistate.repeatCount = 100;
  598. Anistate = Ani.playAdditive(Anistring);
  599. }))
  600. self.Player.runAction(spawn);
  601. funcFlag = true;
  602. }
  603. }
  604. /**
  605. * 返回键监听
  606. * @param event 按下返回键退出游戏
  607. */
  608. AniPlayer(Ani, Anistate, moveByTime, moveByDes, Key, AniName, self?) {
  609. Ani = Key.getComponent(cc.Animation);
  610. let spawn = cc.spawn(cc.callFunc(function () {
  611. self.Player.runAction(cc.moveBy(moveByTime, moveByDes, 0));
  612. }), cc.callFunc(function () {
  613. Anistate = Ani.play(AniName);
  614. Anistate.speed = 2;
  615. Anistate.repeatCount = 100;
  616. }))
  617. self.Player.runAction(spawn);
  618. if (!Ani.state) {
  619. let spawn2 = cc.spawn(cc.callFunc(function () {
  620. self.Player.runAction(cc.moveBy(moveByTime, moveByDes, 0));
  621. }), cc.callFunc(function () {
  622. Anistate = Ani.play(AniName);
  623. Anistate.speed = 2;
  624. Anistate.repeatCount = 100;
  625. }))
  626. self.Player.runAction(spawn2);
  627. }
  628. }
  629. /**
  630. * 抬起动画停止
  631. * @param event 抬起左右键触发
  632. */
  633. onKeyUp(event) {
  634. let self = this;
  635. self.LkeyDown = false;
  636. this.RkeyDown = false;
  637. self.Player.stopAllActions();
  638. let stand = this.Player.getChildByName("stand");
  639. let runRight = this.Player.getChildByName("runRight");
  640. let run = this.Player.getChildByName("run");
  641. stand.active = true;
  642. run.active = false;
  643. runRight.active = false;
  644. }
  645. /**
  646. * gameover
  647. */
  648. gameOver() {
  649. let self = this;
  650. self.Player.stopAllActions();
  651. Global.instance.CollisionFlag = false;
  652. // self.Player.getComponent(cc.BoxCollider).enabled = false;
  653. self.Player.getComponent("Playcontroler").enabled = false;
  654. try {
  655. self.Player.getComponent(cc.Animation).stop();
  656. } catch (error) {
  657. self.node.getChildByName("output").getComponent(cc.Label).string = error;
  658. }
  659. self.Player.getChildByName("stand").active = true;
  660. self.Player.getChildByName("runRight").active = false;
  661. self.Player.getChildByName("run").active = false;
  662. self.Player.y -= Global.instance.InitSpeed;
  663. let AllFH = self.node.getChildByName("BgNode").getChildByName("FHolder").children;
  664. for (let i = 0; i < AllFH.length; i++) {
  665. let FHNA = AllFH[i].name;
  666. AllFH[i].getComponent(FHNA).enabled = false;
  667. AllFH[i].isHold = false;
  668. if (FHNA == "GD") {
  669. AllFH[i].getChildByName("gd").getComponent("CliGD").enabled = false;
  670. }
  671. }
  672. self.StopAni(self);
  673. let failure;
  674. let Ani;
  675. let Anistate;
  676. let BestScore
  677. let thisScore;
  678. let lessScore;
  679. let Score;
  680. // let fuhuo;
  681. let FAni;
  682. let FAnistate;
  683. self.LEFT.node.active = false;
  684. self.RIGHT.node.active = false;
  685. failure = cc.instantiate(self.failure);
  686. // fuhuo = cc.instantiate(self.fuhuo);
  687. // let fhuoBtnRank = fuhuo.getChildByName("cd").getChildByName("RankScene").getComponent(cc.Button);
  688. let fhuoBtnreGame = failure.getChildByName("reset").getComponent(cc.Button);
  689. // let fhuoBtnreGame = fuhuo.getChildByName("zl").getChildByName("rePlay").getComponent(cc.Button);
  690. // let fhuoBtnFhuo = fuhuo.getChildByName("fh").getChildByName("rePlay").getComponent(cc.Button);
  691. let RankEventHandler = new cc.Component.EventHandler();
  692. let reGameEventHandler = new cc.Component.EventHandler();
  693. let FhuoEventHandler = new cc.Component.EventHandler();
  694. //open the ranking
  695. // RankEventHandler.target = self.node;
  696. // RankEventHandler.component = "MainScene";
  697. // RankEventHandler.handler = "FBtnCB_Rank";
  698. // RankEventHandler.customEventData = null;
  699. // fhuoBtnRank.clickEvents.push(RankEventHandler);
  700. //replay
  701. reGameEventHandler.target = self.node;
  702. reGameEventHandler.component = "MainScene";
  703. reGameEventHandler.handler = "restart";
  704. reGameEventHandler.customEventData = null;
  705. fhuoBtnreGame.clickEvents.push(reGameEventHandler);
  706. //fh立即复活会有其他功能暂时用重新开始代替
  707. // FhuoEventHandler.target = self.node;
  708. // FhuoEventHandler.component = "MainScene";
  709. // FhuoEventHandler.handler = "FBtnCB_rePlay";
  710. // FhuoEventHandler.customEventData = null;
  711. // fhuoBtnFhuo.clickEvents.push(FhuoEventHandler);
  712. if (!Global.instance.OverFlag) {
  713. Global.instance.OverFlag = true;
  714. self.StoregeScore();
  715. // self.UpdateScore();
  716. failure.y = -20;
  717. // fuhuo.y = 0;
  718. thisScore = parseInt(self.LifeDing.getChildByName("Floor").getComponent(cc.Label).string);
  719. // lessScore = failure.getChildByName("jl").getChildByName("LessScore");
  720. BestScore = Global.instance.getLocalScore().BestScore;
  721. Global.instance.GetUSer().score = BestScore;
  722. self.UpLoadScore();
  723. if (BestScore - thisScore <= 0) {
  724. failure.getChildByName("jl").active = false;
  725. }
  726. else {
  727. // lessScore.getComponent(cc.Label).string = (BestScore - thisScore).toString();
  728. }
  729. Score = failure.getChildByName("cj").getChildByName("Score");
  730. self.node.addChild(failure);
  731. // self.node.addChild(fuhuo);
  732. Score.getComponent(cc.Label).string = self.LifeDing.getChildByName("Floor").getComponent(cc.Label).string;
  733. // Ani = failure.getComponent(cc.Animation);//the animation of failure;
  734. // // FAni = fuhuo.getComponent(cc.Animation);
  735. // Anistate = Ani.play("shibai");//the state of Ani;
  736. // Anistate.speed = 1;
  737. // Anistate.repeatCount = 1;
  738. }
  739. else {
  740. return;
  741. }
  742. self.playdeadsound();
  743. console.log("游戏结束!!!");
  744. }
  745. /**
  746. * 复活按钮的回调函数:打开排行榜
  747. */
  748. FBtnCB_Rank(self?) {
  749. this.destroy();
  750. cc.director.loadScene("EndScene");
  751. }
  752. /**
  753. * 复活按钮的回调函数:立即复活
  754. */
  755. FBtnCB_rePlay() {
  756. cc.sys.localStorage.setItem("hfsj", "99");
  757. let self = this;
  758. Global.instance.OverFlag = false;
  759. Global.instance.AniFalg = false;
  760. Global.instance.LorR = 0;
  761. Global.instance.InitSpeed = 8.5;
  762. Global.instance.moveSpeed = 1;
  763. // Global.instance.FHFallSpeed = 2;
  764. Global.instance.CollisionFlag = false;
  765. Global.instance.CollisionWithDing = false;
  766. Global.instance.TheHolder = null;
  767. Global.instance.KIND_FootHold = 0;
  768. // this.RIGHT.node.off(cc.Node.EventType.TOUCH_START,this.BtnTurnRight,this);
  769. // this.RIGHT.node.off(cc.Node.EventType.TOUCH_MOVE,this.BtnTurnRight,this);
  770. // this.RIGHT.node.off(cc.Node.EventType.TOUCH_CANCEL,this.onKeyUp,this);
  771. this.LEFT.node.active = true;
  772. this.RIGHT.node.active = true;
  773. this.LkeyDown = false;
  774. this.RkeyDown = false;
  775. this.moveFalg = "";
  776. this.Player.y = 300;
  777. this.Player.x = 0;
  778. this.Player.getComponent("Playcontroler").enabled = true;
  779. this.Player.getComponent(cc.BoxCollider).enabled = true;
  780. let AllFH = this.node.getChildByName("BgNode").getChildByName("FHolder").children;
  781. this.node.getChildByName("SB").destroy();
  782. this.node.getChildByName("fuhuo1").destroy();
  783. for (let i = 0; i < this.LifeDing.children.length; i++) {
  784. //for(let i=0;i<2;i++){
  785. if (this.LifeDing.children[i].name == "lifeBG") {
  786. if (!this.LifeDing.children[i].active) {
  787. this.LifeDing.children[i].active = true;
  788. }
  789. Global.instance.reLife.push(this.LifeDing.children[i]);
  790. }
  791. }
  792. this.Player.getComponent(cc.BoxCollider).enabled = false;
  793. for (let i = 0; i < AllFH.length; i++) {
  794. AllFH[i].destroy();
  795. }
  796. let F = this.FootHoldGenerator(1);
  797. F.y = -380;
  798. this.scheduleOnce(() => {
  799. self.Player.getComponent(cc.BoxCollider).enabled = true;
  800. }, 0.5);
  801. }
  802. /**
  803. * restart
  804. */
  805. restart() {
  806. this.destroy();
  807. cc.director.loadScene("MainScene");
  808. cc.director.resume();
  809. Global.instance.OverFlag = false;
  810. Global.instance.AniFalg = false;
  811. Global.instance.LorR = 0;
  812. Global.instance.InitSpeed = 8.5;
  813. Global.instance.moveSpeed = 1;
  814. Global.instance.FHFallSpeed = 2;
  815. Global.instance.CollisionFlag = false;
  816. Global.instance.CollisionWithDing = false;
  817. Global.instance.TheHolder = null;
  818. Global.instance.KIND_FootHold = 0;
  819. // this.RIGHT.node.off(cc.Node.EventType.TOUCH_START,this.BtnTurnRight,this);
  820. // this.RIGHT.node.off(cc.Node.EventType.TOUCH_MOVE,this.BtnTurnRight,this);
  821. // this.RIGHT.node.off(cc.Node.EventType.TOUCH_CANCEL,this.onKeyUp,this);
  822. this.LEFT.node.active = true;
  823. this.RIGHT.node.active = true;
  824. this.LkeyDown = false;
  825. this.RkeyDown = false;
  826. // this.moveFalg = "";
  827. this.Player.y = 300;
  828. // this.Score();
  829. }
  830. /**
  831. * 受到伤害,命数减一
  832. */
  833. reduceLife() {
  834. let self = this;
  835. let Ls = new Array();
  836. let reLCount = 0;
  837. let FHolder = self.node.getChildByName("BgNode").getChildByName("FHolder").children;
  838. for (let i = 0; i < FHolder.length; i++) {
  839. if (FHolder[i].isHold) {
  840. if (!Global.instance.CollisionFlag) {
  841. FHolder[i].isHold = false;
  842. let box = FHolder[i].getComponent(cc.BoxCollider);
  843. box.enabled = false;
  844. }
  845. }
  846. }
  847. //
  848. for (let i = self.LifeDing.children.length - 1; i >= 0; i--) {
  849. if (self.LifeDing.children[i].name == "lifeBG") {
  850. if (Global.instance.Injured || Global.instance.CollisionWithDing) {
  851. if (self.LifeDing.children[i].active) {
  852. self.LifeDing.children[i].active = false;
  853. Global.instance.Injured = false;
  854. Global.instance.CollisionFlag = false;
  855. Global.instance.CollisionWithDing = false;
  856. }
  857. }
  858. Ls.push(self.LifeDing.children[i]);
  859. }
  860. }
  861. Global.instance.reLife = [];
  862. for (let i = 0; i < Ls.length; i++) {
  863. if (Ls[i].active) {
  864. Global.instance.reLife.push(Ls[i]);//player剩余的命数
  865. }
  866. }
  867. //
  868. var time = cc.sys.localStorage.getItem("hfsj");
  869. //console.log( time + "当前血 : " + Global.instance.reLife.length);
  870. if (Global.instance.reLife.length < 3) {
  871. if (time == 99) {
  872. let time = Math.floor(Date.now() / 1000);
  873. cc.sys.localStorage.setItem("hfsj", time);
  874. } else {
  875. let time = Math.floor(Date.now() / 1000);
  876. let timlod = parseInt(cc.sys.localStorage.getItem("hfsj"));
  877. let sub = time - timlod;
  878. if (sub >= 20) {
  879. let sizes = Global.instance.reLife.length + 2;
  880. Global.instance.reLife = [];
  881. for (let i = 0; i < sizes; i++) {
  882. if (this.LifeDing.children[i].name == "lifeBG") {
  883. if (!this.LifeDing.children[i].active) {
  884. this.LifeDing.children[i].active = true;
  885. }
  886. Global.instance.reLife.push(this.LifeDing.children[i]);
  887. // console.log("push --------------" + i);
  888. }
  889. }
  890. cc.sys.localStorage.setItem("hfsj", time);
  891. // console.log(sizes + "恢复血+1---------------------------------------" + Global.instance.reLife.length);
  892. }
  893. }
  894. } else {
  895. cc.sys.localStorage.setItem("hfsj", "99");
  896. }
  897. }
  898. /**
  899. * 每下落一层,分数加一。
  900. */
  901. Score() {
  902. let self = this.node;
  903. let scLabel = this.LifeDing.getChildByName("Floor").getComponent(cc.Label);
  904. let sc = parseInt(scLabel.string);
  905. if (!Global.instance.OverFlag) {
  906. sc = sc + 1;
  907. }
  908. this.playtansound();
  909. console.log("打印一下");
  910. scLabel.string = sc.toString();
  911. let lv = Math.ceil(sc / 50);
  912. if (lv > Global.instance.LevelAddFlag) {
  913. Global.instance.LevelAddFlag = lv;
  914. Global.instance.InitSpeed += 0.25;//每下落50层,player下落速度加0.25
  915. Global.instance.FHFallSpeed += 1;//没下落50层,落脚点下落速度加快0.5.
  916. }
  917. }
  918. /**
  919. * 本地分数存储
  920. */
  921. StoregeScore() {
  922. let self = this;
  923. let localS = Global.instance.getLocalScore();//the temp of local score;
  924. let CurrentScore = parseInt(
  925. this.LifeDing.getChildByName("Floor").getComponent(cc.Label).string);
  926. Global.instance.getLocalScore().CurrentScore = CurrentScore;
  927. if (CurrentScore > localS.ThirdScore) {
  928. if (CurrentScore > localS.SecondScore) {
  929. if (CurrentScore > localS.BestScore) {
  930. localS.BestScore = CurrentScore;
  931. return;
  932. }
  933. localS.SecondScore = CurrentScore;
  934. }
  935. localS.ThirdScore = CurrentScore;
  936. }
  937. }
  938. /**
  939. * upload the best score
  940. */
  941. UpLoadScore() {
  942. let self = this;
  943. let tmp = Global.instance.GetUSer();
  944. /* Http.sendRequest("/updateScore",tmp,function(msg){
  945. let x = msg;
  946. if(msg.code!=0){
  947. Global.instance.NetStatus = false;
  948. }
  949. let ux = {
  950. userId:'',
  951. }
  952. console.log(x);
  953. ux.userId = Global.instance.GetUSer().userId;
  954. Http.sendRequest("/rank",ux,function(mmsg){
  955. console.log("------------------3");
  956. let MM = JSON.parse(mmsg.msg);
  957. console.log(MM);
  958. if(Global.instance.GetR()==null||Global.instance.GetR().rank==undefined||Global.instance.GetR().rank<MM.my.rank){
  959. Global.instance.SetR(MM.my.rank);
  960. Global.instance.setRemoteScore(MM.other);
  961. }
  962. });
  963. });*/
  964. }
  965. }