MainScene.ts 40 KB

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