MainScene.ts 35 KB

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