import Global from "./Global" import Http from "./Http"; const {ccclass, property} = cc._decorator; @ccclass export default class MainScene extends cc.Component { @property(cc.Prefab) boli:cc.Prefab = null; @property(cc.Prefab) dici:cc.Prefab = null; @property(cc.Prefab) lvdai:cc.Prefab = null; @property(cc.Prefab) shandian:cc.Prefab = null; @property(cc.Prefab) tanhuang:cc.Prefab = null; @property(cc.Prefab) Opplvdai:cc.Prefab = null; @property(cc.Prefab) GD:cc.Prefab = null; @property(cc.Prefab) failure:cc.Prefab = null; @property(cc.Prefab) fuhuo:cc.Prefab = null; @property(cc.Node) Player:cc.Node = null; @property(cc.Node) FHolderNode:cc.Node = null; @property(cc.Node) Bg:cc.Node = null; @property(cc.Node) LifeDing:cc.Node = null; @property(cc.Button) LEFT:cc.Button = null; @property(cc.Button) RIGHT:cc.Button = null; @property(cc.Label) output:cc.Label = null; // LIFE-CYCLE CALLBACKS: /** * 上一个落脚点生成时间 */ private STime = 0; /** * 当前落脚点生成时间 */ private ETime = 0; /** Left*/ private LkeyDown = false; /** Right*/ private RkeyDown = false; /**the loght FootHolder F */ private lightF = false; moveFalg = ""; USERINFO = { userId:"", score:0, } onLoad () { var time = cc.sys.localStorage.getItem("hfsj"); if( time == null || time == undefined || time == "") { cc.sys.localStorage.setItem("hfsj","99"); } this.LifeDing.zIndex = 10; this.FHolderNode.zIndex = 9; Global.instance.setMN(this.node); this.STime = Date.now(); let FHolder = cc.instantiate(this.GD); this.FHolderNode.addChild(FHolder,10,"GD"); FHolder.getComponent("GD").init(this,1); FHolder.y = -150; let FHolder2 = cc.instantiate(this.lvdai); this.FHolderNode.addChild(FHolder2,10,"lvdai"); FHolder2.getComponent("lvdai").init(this); FHolder2.y = -300; // let FHolder2 = cc.instantiate(this.tanhuang); // this.FHolderNode.addChild(FHolder2,10,"tanhuang"); // FHolder2.getComponent("tanhuang").init(this); // FHolder2.y = -300; let FHolder3 = cc.instantiate(this.GD); this.FHolderNode.addChild(FHolder3,10,"GD"); FHolder3.getComponent("GD").init(this,1); FHolder3.y = -450; // this.Player.active = false; this.Player.x = FHolder.x; // this.Player.y = FHolder.y+60; this.Player.zIndex = 11; for(let i=0;i(1200-(180*(Global.instance.FHFallSpeed-2)))){//控制落脚点之间的间距,间距144px this.STime = Date.now(); if(!Global.instance.OverFlag){ FHolder = this.FootHoldGenerator(null); // this.Score(); } } this.FHolder(); this.reduceLife(); if(Global.instance.CollisionFlag){//左右传送带减速 switch(Global.instance.KIND_FootHold){ case 3:{ this.Player.x += 2; break; } case 4:{ this.Player.x -= 2; break; } case 7:{ if(Global.instance.LorR==1){ this.Player.x --; } else{ this.Player.x ++; } break; } default:{ break; } } } if(this.Player.x<-180){ this.Player.x = -180; } if(this.Player.x>180){ this.Player.x = 180; } if(this.Player.y<(-510)){ for(let i=0;i{ this.onKeyUp; },this); } else{ this.RIGHT.node.off(cc.Node.EventType.TOUCH_START,this.BtnTurnRight,this); this.RIGHT.node.off(cc.Node.EventType.TOUCH_MOVE,this.BtnTurnRight,this); this.RIGHT.node.off(cc.Node.EventType.TOUCH_CANCEL,this.onKeyUp,this); this.RIGHT.node.off(cc.Node.EventType.TOUCH_END,()=>{ this.onKeyUp; },this); } if(!this.RkeyDown){ this.LEFT.node.on(cc.Node.EventType.TOUCH_START,this.BtnTurnLeft,this); this.LEFT.node.on(cc.Node.EventType.TOUCH_MOVE,this.BtnTurnLeft,this); this.LEFT.node.on(cc.Node.EventType.TOUCH_CANCEL,this.onKeyUp,this); // btnClickArr.push(this.LEFT); this.LEFT.node.on(cc.Node.EventType.TOUCH_END,()=>{ this.onKeyUp; },this); } else{ this.LEFT.node.off(cc.Node.EventType.TOUCH_START,this.BtnTurnLeft,this); this.LEFT.node.off(cc.Node.EventType.TOUCH_MOVE,this.BtnTurnLeft,this); this.LEFT.node.off(cc.Node.EventType.TOUCH_CANCEL,this.onKeyUp,this); this.LEFT.node.off(cc.Node.EventType.TOUCH_END,()=>{ this.onKeyUp; },this); } } StopAni(self){ if(!Global.instance.AniFalg){ let FHolder = self.node.getChildByName("BgNode").getChildByName("FHolder").children; let Fname; for(let i=0;FHolder.length;i++){ if(i == FHolder.length-1){ Global.instance.AniFalg = true; return; } Fname = FHolder[i].name; FHolder[i].getComponent(Fname).enabled = false; } } } /** * player 移出落脚点 */ FHolder(){ let self = this let FHArray = this.FHolderNode.children; let Ani;//the Animation of Player when player fall down let Anistate;//the state of Ani; for(let i=FHArray.length-1;i>=0;i--){ if(FHArray[i].isHold){ // let nameNode:string = FHArray[i].name; if(self.Player.x>(FHArray[i].x+75)){ Global.instance.CollisionFlag = false; FHArray[i].isHold = false; // this.Score(); } else if(self.Player.x<(FHArray[i].x-75)){ Global.instance.CollisionFlag = false; FHArray[i].isHold = false; // this.Score(); } else{ Global.instance.CollisionFlag = true; FHArray[i].isHold = true; } } } } /** * 移动背景,初始移动速度200px/s */ MoveBg(){ let Bg0 = this.Bg.getChildByName("Bg_0"); let Bg1 = this.Bg.getChildByName("Bg_1"); if(Global.instance.OverFlag){ } else{ Bg0.y+=Global.instance.FHFallSpeed+1; Bg1.y+=Global.instance.FHFallSpeed+1; if(Bg0.y>946){ Bg0.y = -946; } if(Bg1.y>946){ Bg1.y = -946; } } } /** * 生成落脚点 */ FootHoldGenerator(rtpe){ let self = this; let Magnification = 0; let f = parseInt(self.LifeDing.getChildByName("Floor").getComponent(cc.Label).string); if(f<20){ Magnification = 2 } else if(f<40){ Magnification = 4; } else if(f<60){ Magnification = 6; } else if(f<80){ Magnification = 8; } else{ Magnification = 8; } let KindHolder = Math.ceil(Math.random()*Magnification); // KindHolder = 7; let FHolder; // KindHolder = Math.ceil(Math.random()*7); if( KindHolder == 3 || KindHolder == 4 ) { let va = Math.random()*10; if( va >=1 && va < 4 ) { KindHolder = 7; }else if( va >=4 && va < 10 ) { KindHolder = 5; } //KindHolder = 5; console.log("KindHolder gb: " + KindHolder); } if( rtpe != null ) { KindHolder = rtpe; console.log("KindHolder gb2: " + KindHolder); } this.ETime = Date.now(); switch(KindHolder){ case 1:{ FHolder = cc.instantiate(self.GD); self.FHolderNode.addChild(FHolder,5,"GD"); FHolder.getComponent("GD").init(self,1); FHolder.isHold = false; // console.log("产生第一种落脚点"); break; } case 2:{ FHolder = cc.instantiate(self.GD); self.FHolderNode.addChild(FHolder,5,"GD"); FHolder.getComponent("GD").init(self,7); FHolder.isHold = false; /*self.FootHoldGenerator(); FHolder = cc.instantiate(self.tanhuang); self.FHolderNode.addChild(FHolder,5,"tanhuang"); FHolder.getComponent("tanhuang").init(self); FHolder.isHold = false; // // console.log("产生第五种落脚点"); // break;*/ break; } case 3:{ FHolder = cc.instantiate(self.lvdai); self.FHolderNode.addChild(FHolder,5,"lvdai"); FHolder.getComponent("lvdai").init(self); FHolder.isHold = false; // console.log("产生第二种落脚点"); console.log( " 创建滚动 " ); break; } case 4:{ FHolder = cc.instantiate(self.lvdai); self.FHolderNode.addChild(FHolder,5,"lvdai"); FHolder.getComponent("lvdai").init(self); FHolder.isHold = false; // console.log("产生第五种落脚点"); console.log( " 创建滚动1 " ); break; } case 5:{ FHolder = cc.instantiate(self.boli); self.FHolderNode.addChild(FHolder,5,"boli"); FHolder.getComponent("boli").init(self); FHolder.isHold = false; // console.log("产生第三种落脚点"); break; } case 6:{ FHolder = cc.instantiate(self.dici); self.FHolderNode.addChild(FHolder,5,"dici"); FHolder.getComponent("dici").init(self); FHolder.isHold = false; // console.log("产生第四种落脚点"); break; } case 7:{ FHolder = cc.instantiate(self.GD); self.FHolderNode.addChild(FHolder,5,"GD"); FHolder.getComponent("GD").init(self,7); FHolder.isHold = false; break; } case 8:{ if(self.lightF){ self.lightF = false; self.FootHoldGenerator(null); break; } FHolder = cc.instantiate(self.shandian); self.FHolderNode.addChild(FHolder,5,"shandian"); FHolder.getComponent("shandian").init(self); FHolder.isHold = false; self.lightF = true; break; } // default:{ // FHolder = cc.instantiate(self.GD); // self.FHolderNode.addChild(FHolder,5,"GD"); // FHolder.getComponent("GD").init(self); // FHolder.getComponent("GD").enabled = true; // FHolder.getComponent("MoveGD").init(self); // FHolder.getComponent("MoveGD").enabled = false; // FHolder.isHold = false; // // console.log("默认产生第一种落脚点"); // } // break; } return FHolder; } /** * 按钮触发,向左,长按事件 */ BtnTurnLeft(event){ let self = this; if(self.RkeyDown){ return; } else{ self.LkeyDown = true; self.RkeyDown = false; this.BtnLorR(event); } } /** * 按钮触发,向右,长按事件,长按响应未解决 */ BtnTurnRight(event){ let self = this; if(self.LkeyDown){ return; } else{ self.LkeyDown = false self.RkeyDown = true; this.BtnLorR(event); } } /** * btn触发事件 * @param event 按钮触发事件 */ BtnLorR(event){ let self = this; Global.instance.moveSpeed = 1; let schedule = cc.director.getScheduler(); let stand = self.Player.getChildByName("stand"); let runRight = self.Player.getChildByName("runRight"); let run = self.Player.getChildByName("run"); let Ani;//the animation of player let Anistate;//the Ani's state let Anistring;//the name of Ani let moveByTime = 1; let scheduleState:boolean = false;//the schedule's state let schedulePause:boolean = true; let target:cc.Button = null;//the target which is binged to schedule let moveByDes = Global.instance.moveSpeed*120+40; if(self.LkeyDown){ target = self.LEFT; moveByTime = 1; moveByDes = -moveByDes; scheduleState = schedule.isScheduled(func,target); // self.output.getComponent(cc.Label).string = Anistring; schedulePause = schedule.isTargetPaused(target); Ani = run.getComponent(cc.Animation); Anistring = "run"; stand.active = false; runRight.active = false; run.active = true; // self.output.getComponent(cc.Label).string = Anistring; if(!Global.instance.CollisionFlag){ moveByTime = 1; } else{ switch(Global.instance.KIND_FootHold){ case 2:{ moveByTime = 4.1; self.output.getComponent(cc.Label).string = Global.instance.KIND_FootHold+"2"; break; } case 5:{ moveByTime = 3.9; // self.output.getComponent(cc.Label).string = Global.instance.KIND_FootHold+"5"; break; } }; } } if(self.RkeyDown){ target = self.RIGHT; moveByTime = 1; moveByDes = moveByDes; scheduleState = schedule.isScheduled(func,target); // self.output.getComponent(cc.Label).string = Anistring; schedulePause = schedule.isTargetPaused(target); Ani = runRight.getComponent(cc.Animation); Anistring = "runR"; stand.active = false; runRight.active = true; run.active = false; // self.output.getComponent(cc.Label).string = Anistring; if(!Global.instance.CollisionFlag){ moveByTime = 1; } else{ switch(Global.instance.KIND_FootHold){ case 2:{ moveByTime = 4.1; // self.output.getComponent(cc.Label).string = Global.instance.KIND_FootHold+"2"; break; } case 5:{ moveByTime = 3.9; // self.output.getComponent(cc.Label).string = Global.instance.KIND_FootHold+"5"; break; } }; } } let funcFlag = false; switch(event.type){ case "touchstart":{ if(!(self.moveFalg.length==0)){ break; } self.moveFalg = "touchstart"; if(!scheduleState){ // self.output.getComponent(cc.Label).string = movefalg; schedule.schedule(func,target,0); }; if(schedulePause){ schedule.resumeTarget(target); }; break; } case "touchmove":{ if(!(self.moveFalg.length==0)){ break; } self.moveFalg = "touchmove"; // self.output.getComponent(cc.Label).string = movefalg; if(!scheduleState){ schedule.schedule(func,target,0); }; if(schedulePause){ schedule.resumeTarget(target); }; break; } case "touchcancel":{ break; } case "touchend":{ // self.output.getComponent(cc.Label).string = "touchend"; schedule.pauseTarget(target); self.Player.stopAllActions(); Ani.stop(Anistring); moveByTime = 1; moveByDes = Global.instance.moveSpeed*120+40; switch(Anistring){ case "run":{ run.active = false; runRight.active = false; stand.active = true; self.LkeyDown = false; self.RkeyDown = false; moveByDes = Global.instance.moveSpeed/2; break; } case "runR":{ run.active = false; runRight.active = false; stand.active = true; self.LkeyDown = false; self.RkeyDown = false; moveByDes = Global.instance.moveSpeed/2; break; } } funcFlag = false; self.moveFalg = ""; break; } } function func(){ if(funcFlag){ return; } if(Global.instance.CollisionFlag){ moveByTime = 1; } let spawn = cc.spawn(cc.callFunc(function(){ self.Player.runAction(cc.moveBy(moveByTime,moveByDes,0)); }),cc.callFunc(function(){ Anistate = Ani.play(Anistring); Anistate.speed = 1; Anistate.repeatCount = 100; Anistate = Ani.playAdditive(Anistring); })) self.Player.runAction(spawn); funcFlag = true; } } /** * 返回键监听 * @param event 按下返回键退出游戏 */ AniPlayer(Ani,Anistate,moveByTime,moveByDes,Key,AniName,self?){ Ani = Key.getComponent(cc.Animation); let spawn = cc.spawn(cc.callFunc(function(){ self.Player.runAction(cc.moveBy(moveByTime,moveByDes,0)); }),cc.callFunc(function(){ Anistate = Ani.play(AniName); Anistate.speed = 2; Anistate.repeatCount = 100; })) self.Player.runAction(spawn); if(!Ani.state){ let spawn2 = cc.spawn(cc.callFunc(function(){ self.Player.runAction(cc.moveBy(moveByTime,moveByDes,0)); }),cc.callFunc(function(){ Anistate = Ani.play(AniName); Anistate.speed = 2; Anistate.repeatCount = 100; })) self.Player.runAction(spawn2); } } /** * 抬起动画停止 * @param event 抬起左右键触发 */ onKeyUp(event){ let self = this; self.LkeyDown = false; this.RkeyDown = false; self.Player.stopAllActions(); let stand = this.Player.getChildByName("stand"); let runRight = this.Player.getChildByName("runRight"); let run = this.Player.getChildByName("run"); stand.active = true; run.active = false; runRight.active = false; } /** * gameover */ gameOver(){ let self = this; self.Player.stopAllActions(); Global.instance.CollisionFlag = false; // self.Player.getComponent(cc.BoxCollider).enabled = false; self.Player.getComponent("Playcontroler").enabled = false; try { self.Player.getComponent(cc.Animation).stop(); } catch (error) { self.node.getChildByName("output").getComponent(cc.Label).string = error; } self.Player.getChildByName("stand").active = true; self.Player.getChildByName("runRight").active = false; self.Player.getChildByName("run").active = false; self.Player.y-=Global.instance.InitSpeed; let AllFH = self.node.getChildByName("BgNode").getChildByName("FHolder").children; for(let i=0;i{ self.Player.getComponent(cc.BoxCollider).enabled = true; },0.5); } /** * restart */ restart(){ this.destroy(); cc.director.loadScene("MainScene"); cc.director.resume(); Global.instance.OverFlag = false; Global.instance.AniFalg = false; Global.instance.LorR = 0; Global.instance.InitSpeed = 8.5; Global.instance.moveSpeed = 1; Global.instance.FHFallSpeed = 2; Global.instance.CollisionFlag = false; Global.instance.CollisionWithDing = false; Global.instance.TheHolder = null; Global.instance.KIND_FootHold = 0; // this.RIGHT.node.off(cc.Node.EventType.TOUCH_START,this.BtnTurnRight,this); // this.RIGHT.node.off(cc.Node.EventType.TOUCH_MOVE,this.BtnTurnRight,this); // this.RIGHT.node.off(cc.Node.EventType.TOUCH_CANCEL,this.onKeyUp,this); this.LEFT.node.active = true; this.RIGHT.node.active = true; this.LkeyDown = false; this.RkeyDown = false; // this.moveFalg = ""; this.Player.y = 300; // this.Score(); } /** * 受到伤害,命数减一 */ reduceLife(){ let self = this; let Ls = new Array(); let reLCount=0; let FHolder = self.node.getChildByName("BgNode").getChildByName("FHolder").children; for(let i=0;i=0;i--){ if(self.LifeDing.children[i].name=="lifeBG"){ if(Global.instance.Injured||Global.instance.CollisionWithDing){ if(self.LifeDing.children[i].active){ self.LifeDing.children[i].active = false; Global.instance.Injured = false; Global.instance.CollisionFlag = false; Global.instance.CollisionWithDing = false; } } Ls.push(self.LifeDing.children[i]); } } Global.instance.reLife = []; for(let i=0;i=20 ) { let sizes = Global.instance.reLife.length+2; Global.instance.reLife = []; for(let i=0;iGlobal.instance.LevelAddFlag){ Global.instance.LevelAddFlag = lv; Global.instance.InitSpeed+=0.25;//每下落50层,player下落速度加0.25 Global.instance.FHFallSpeed+=1;//没下落50层,落脚点下落速度加快0.5. } } /** * 本地分数存储 */ StoregeScore(){ let self = this; let localS = Global.instance.getLocalScore();//the temp of local score; let CurrentScore = parseInt( this.LifeDing.getChildByName("Floor").getComponent(cc.Label).string); Global.instance.getLocalScore().CurrentScore = CurrentScore; if(CurrentScore>localS.ThirdScore){ if(CurrentScore>localS.SecondScore){ if(CurrentScore>localS.BestScore){ localS.BestScore = CurrentScore; return; } localS.SecondScore = CurrentScore; } localS.ThirdScore = CurrentScore; } } /** * upload the best score */ UpLoadScore(){ let self = this; let tmp = Global.instance.GetUSer(); /* Http.sendRequest("/updateScore",tmp,function(msg){ let x = msg; if(msg.code!=0){ Global.instance.NetStatus = false; } let ux = { userId:'', } console.log(x); ux.userId = Global.instance.GetUSer().userId; Http.sendRequest("/rank",ux,function(mmsg){ console.log("------------------3"); let MM = JSON.parse(mmsg.msg); console.log(MM); if(Global.instance.GetR()==null||Global.instance.GetR().rank==undefined||Global.instance.GetR().rank