1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234 |
- import Global from "./Global"
- import Http from "./Http";
- import MessManager from "./MessManager";
- 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,
- }
- fuhuoCount = 1
- onKeyDown(event) {
- if (event.keyCode == cc.macro.KEY.w) {
- console.error(1231);
- }
- }
- onLoad() {
- cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
- MessManager.getInstance().initData()
- 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.getComponent("GD").First = true
- FHolder.y = -100;
- 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.dici);
- // this.FHolderNode.addChild(FHolder2, 10, "dici");
- // FHolder2.getComponent("dici").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 = 0;
- // this.Player.y = FHolder.y+60;
- this.Player.zIndex = 11;
- for (let i = 0; i < this.LifeDing.children.length; i++) {
- if (this.LifeDing.children[i].name == "lifeBG") {
- if (!this.LifeDing.children[i].active) {
- this.LifeDing.children[i].active = true;
- }
- Global.instance.reLife.push(this.LifeDing.children[i]);
- }
- }
- cc.director.getCollisionManager().enabled = true;
- // cc.director.getCollisionManager().enabledDebugDraw = true;
- cc.director.getCollisionManager().enabledDrawBoundingBox = false;
- }
- playtansound() {
- if (Global.instance.OverFlag == false) {
- cc.loader.loadRes("tan", cc.AudioClip, function (err, clip) {
- var audioID = cc.audioEngine.play(clip, false, 0.5);
- });
- }
- }
- playdeadsound() {
- cc.loader.loadRes("dead", cc.AudioClip, function (err, clip) {
- var audioID = cc.audioEngine.play(clip, false, 0.5);
- });
- }
- start() {
- }
- update(dt) {
- // console.log(Global.instance.CollisionFlag);
- this.MoveBg();
- Global.instance.moveSpeed = 1;
- let FHolder;
- if ((this.ETime - this.STime) > (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 < (-560)) {
- this.Player.getComponent(cc.BoxCollider).enabled = false;
- this.pretreatmentGameover();
- }
- if (Global.instance.reLife.length == 0) {
- this.Player.getComponent(cc.BoxCollider).enabled = false;
- this.pretreatmentGameover();
- }
- this.ETime = Date.now();
- // let AllChildren = new Array();
- // let GDChildren = new Array();
- // AllChildren = this.node.getChildByName("BgNode").getChildByName("FHolder").children;
- // for (let i = 0; i < AllChildren.length; i++) {
- // if (AllChildren[i].name == "GD") {
- // GDChildren.push(AllChildren[i]);
- // }
- // }
- // cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN,this.onKeyDown,this);
- //cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP,this.onKeyUp,this);
- this.Listener();
- }
- Listener() {
- let btnClickArr = new Array(2);
- if (!this.LkeyDown) {
- this.RIGHT.node.on(cc.Node.EventType.TOUCH_START, this.BtnTurnRight, this);
- this.RIGHT.node.on(cc.Node.EventType.TOUCH_MOVE, this.BtnTurnRight, this);
- this.RIGHT.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onKeyUp, this);
- btnClickArr.push(this.RIGHT);
- this.RIGHT.node.on(cc.Node.EventType.TOUCH_END, () => {
- 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) {
- if (self.Player.x > (FHArray[i].x + 75)) {
- Global.instance.CollisionFlag = false;
- FHArray[i].isHold = false;
- }
- else if (self.Player.x < (FHArray[i].x - 75)) {
- Global.instance.CollisionFlag = false;
- FHArray[i].isHold = false;
- }
- 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;
- 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;
- }
- once = false;
- pretreatmentGameover() {
- if (!Global.instance.OverFlag && this.once == false) {
- this.once = true;
- MessManager.getInstance().getUserSore(
- (res) => {
- console.error('收到正常消息一次');
- // console.error(res);
- if (this.fuhuoCount-- > 0) {
- this.showFail(res.data.thirty_score, res.data.one_hundred_score)
- } else {
- this.gameOver(res.data.thirty_score, res.data.one_hundred_score)
- this.once = false;
- }
- },
- () => {
- console.error('收到失败消息一次');
- this.gameOver()
- this.once = false;
- }
- )
- }
- }
- showFailPnael() {
- let failure = cc.instantiate(this.failure);
- let ReStartBtnreGame = failure.getChildByName("reset").getComponent(cc.Button);
- let reGameEventHandler = new cc.Component.EventHandler();
- reGameEventHandler.target = this.node;
- reGameEventHandler.component = "MainScene";
- reGameEventHandler.handler = "restart";
- reGameEventHandler.customEventData = null;
- ReStartBtnreGame.clickEvents.push(reGameEventHandler);
- }
- showFail(score1 = 0, score2 = 0) {
- let self = this;
- let failure;
- let Score;
- failure = cc.instantiate(self.failure);
- let ReStartBtnreGame = failure.getChildByName("reset").getComponent(cc.Button);
- let fuhuo = failure.getChildByName("fuhuo").getComponent(cc.Button);
- failure.getChildByName("fuhuo").active = true
- this.init2Btn(fuhuo, ReStartBtnreGame)
- if (!Global.instance.OverFlag) {
- Global.instance.OverFlag = true;
- this.Player.active = false
- failure.y = -20;
- self.node.addChild(failure);
- Score = failure.getChildByName("cj").getChildByName("Score");
- Score.getComponent(cc.Label).string = self.LifeDing.getChildByName("Floor").getComponent(cc.Label).string;
- if (parseInt(Score.getComponent(cc.Label).string) >= 100) {
- failure.getChildByName("fuhuo").active = false
- } else {
- failure.getChildByName("fuhuo").active = true
- }
- let TipSNode = failure.getChildByName("sb")
- this.initFailStr(Score, TipSNode, failure, score1, score2)
- }
- else {
- return;
- }
- }
- /**
- *
- */
- gameOver(score1 = 0, score2 = 0) {
- 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 < AllFH.length; i++) {
- let FHNA = AllFH[i].name;
- AllFH[i].getComponent(FHNA).enabled = false;
- AllFH[i].isHold = false;
- if (FHNA == "GD") {
- AllFH[i].getChildByName("gd").getComponent("CliGD").enabled = false;
- }
- }
- self.StopAni(self);
- for (let i = 0; i < this.LifeDing.children.length; i++) {
- if (this.LifeDing.children[i].name == "lifeBG") {
- this.LifeDing.children[i].active = false;
- }
- }
- self.LEFT.node.active = false;
- self.RIGHT.node.active = false;
- self.playdeadsound();
- let failure;
- let Score;
- failure = cc.instantiate(self.failure);
- let ReStartBtnreGame = failure.getChildByName("reset").getComponent(cc.Button);
- let fuhuo = failure.getChildByName("fuhuo").getComponent(cc.Button);
- failure.getChildByName("fuhuo").active = false
- this.init2Btn(fuhuo, ReStartBtnreGame)
- if (!Global.instance.OverFlag) {
- Global.instance.OverFlag = true;
- this.Player.active = false
- failure.y = -20;
- self.node.addChild(failure);
- Score = failure.getChildByName("cj").getChildByName("Score");
- Score.getComponent(cc.Label).string = self.LifeDing.getChildByName("Floor").getComponent(cc.Label).string;
- let TipSNode = failure.getChildByName("sb")
- this.initFailStr(Score, TipSNode, failure, score1, score2)
- }
- else {
- return;
- }
- console.log("游戏结束!!!");
- }
- init2Btn(fuhuo: cc.Button, reset: cc.Button) {
- let FuhupEventHandler = new cc.Component.EventHandler();
- FuhupEventHandler.target = this.node;
- FuhupEventHandler.component = "MainScene";
- FuhupEventHandler.handler = "fuhuoOnce";
- FuhupEventHandler.customEventData = null;
- fuhuo.clickEvents.push(FuhupEventHandler);
- let reGameEventHandler = new cc.Component.EventHandler();
- reGameEventHandler.target = this.node;
- reGameEventHandler.component = "MainScene";
- reGameEventHandler.handler = "restart";
- reGameEventHandler.customEventData = null;
- reset.clickEvents.push(reGameEventHandler);
- }
- initFailStr(CurrentScore, tipsNode: cc.Node, failure: cc.Node, score1, score2) {
- if (parseInt(CurrentScore.getComponent(cc.Label).string) >= 100) {
- tipsNode.getComponent(cc.Label).string = "成功"
- } else {
- tipsNode.getComponent(cc.Label).string = "失败"
- }
- failure.getChildByName("tips").active = false;
- failure.getChildByName("tips").getComponent(cc.Label).string = `今日可获得奖励
- 60层过关奖励: ${score1}/90 积分
- 100层通关奖励: ${score2}/100积分`
- failure.getChildByName("tips").active = true;
- }
- fuhuoOnce(a, b, c) {
- let once = () => {
- Global.instance.OverFlag = false;
- let AllFH = this.node.getChildByName("BgNode").getChildByName("FHolder").children;
- for (let i = 0; i < AllFH.length; i++) {
- AllFH[i].destroy();
- }
- for (let i = 0; i < this.LifeDing.children.length; i++) {
- if (this.LifeDing.children[i].name == "lifeBG") {
- if (!this.LifeDing.children[i].active) {
- this.LifeDing.children[i].active = true;
- }
- Global.instance.reLife.push(this.LifeDing.children[i]);
- }
- }
- this.Player.getComponent(cc.BoxCollider).enabled = false;
- this.Player.y = 300;
- this.scheduleOnce(() => {
- Global.instance.CollisionFlag = false;
- this.Player.active = true
- this.Player.getComponent(cc.BoxCollider).enabled = true;
- }, 0.1)
- a.target.parent.destroy();
- this.FootHoldGenerator(1).y = -380;
- this.FootHoldGenerator(1).y = -220;
- this.FootHoldGenerator(1).y = -100;
- this.once = false;
- }
- if (CC_DEBUG) {
- once()
- return
- }
- MessManager.getInstance().changeScore(-10,
- () => {
- once()
- },
- () => {
- once()
- },
- )
- }
- /**
- * 复活按钮的回调函数:打开排行榜
- */
- FBtnCB_Rank(self?) {
- this.destroy();
- cc.director.loadScene("EndScene");
- }
- /**
- * 复活按钮的回调函数:立即复活
- */
- FBtnCB_rePlay() {
- cc.sys.localStorage.setItem("hfsj", "99");
- let self = this;
- Global.instance.OverFlag = false;
- Global.instance.AniFalg = false;
- Global.instance.LorR = 0;
- Global.instance.InitSpeed = Global.instance.ConstInitSpeed
- 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.Player.x = 0;
- this.Player.getComponent("Playcontroler").enabled = true;
- this.Player.getComponent(cc.BoxCollider).enabled = true;
- let AllFH = this.node.getChildByName("BgNode").getChildByName("FHolder").children;
- this.node.getChildByName("SB").destroy();
- // this.node.getChildByName("fuhuo1").destroy();
- for (let i = 0; i < this.LifeDing.children.length; i++) {
- //for(let i=0;i<2;i++){
- if (this.LifeDing.children[i].name == "lifeBG") {
- if (!this.LifeDing.children[i].active) {
- this.LifeDing.children[i].active = true;
- }
- Global.instance.reLife.push(this.LifeDing.children[i]);
- }
- }
- this.Player.getComponent(cc.BoxCollider).enabled = false;
- for (let i = 0; i < AllFH.length; i++) {
- AllFH[i].destroy();
- }
- let F = this.FootHoldGenerator(1);
- F.y = -380;
- this.scheduleOnce(() => {
- 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 = Global.instance.ConstInitSpeed
- 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 < FHolder.length; i++) {
- if (FHolder[i].isHold) {
- if (!Global.instance.CollisionFlag) {
- FHolder[i].isHold = false;
- let box = FHolder[i].getComponent(cc.BoxCollider);
- box.enabled = false;
- }
- }
- }
- //
- for (let i = self.LifeDing.children.length - 1; 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 < Ls.length; i++) {
- if (Ls[i].active) {
- Global.instance.reLife.push(Ls[i]);//player剩余的命数
- }
- }
- //
- var time = cc.sys.localStorage.getItem("hfsj");
- //console.log( time + "当前血 : " + Global.instance.reLife.length);
- if (Global.instance.reLife.length < 3) {
- if (time == 99) {
- let time = Math.floor(Date.now() / 1000);
- cc.sys.localStorage.setItem("hfsj", time);
- } else {
- let time = Math.floor(Date.now() / 1000);
- let timlod = parseInt(cc.sys.localStorage.getItem("hfsj"));
- let sub = time - timlod;
- if (sub >= 20) {
- let sizes = Global.instance.reLife.length + 2;
- Global.instance.reLife = [];
- for (let i = 0; i < sizes; i++) {
- if (this.LifeDing.children[i].name == "lifeBG") {
- if (!this.LifeDing.children[i].active) {
- this.LifeDing.children[i].active = true;
- }
- Global.instance.reLife.push(this.LifeDing.children[i]);
- // console.log("push --------------" + i);
- }
- }
- cc.sys.localStorage.setItem("hfsj", time);
- // console.log(sizes + "恢复血+1---------------------------------------" + Global.instance.reLife.length);
- }
- }
- } else {
- cc.sys.localStorage.setItem("hfsj", "99");
- }
- }
- /**
- * 每下落一层,分数加一。
- */
- Score() {
- let self = this.node;
- let scLabel = this.LifeDing.getChildByName("Floor").getComponent(cc.Label);
- let sc = parseInt(scLabel.string);
- if (!Global.instance.OverFlag) {
- sc = sc + 1;
- }
- this.playtansound();
- console.log("打印一下");
- scLabel.string = sc.toString();
- if (sc == 60) {
- MessManager.getInstance().questServer(30,
- null,
- null
- )
- }
- if (sc == 100) {
- MessManager.getInstance().questServer(100,
- () => {
- this.pretreatmentGameover();
- },
- null
- )
- ///
- }
- let lv = Math.ceil(sc / 25);
- if (lv > Global.instance.LevelAddFlag) {
- Global.instance.LevelAddFlag = lv;
- Global.instance.InitSpeed += 0.5;//每下落50层,player下落速度加0.25
- Global.instance.FHFallSpeed += 1;//没下落50层,落脚点下落速度加快0.5.
- if (sc == 50) {
- Global.instance.InitSpeed += 1;//每下落50层,player下落速度加0.25
- Global.instance.FHFallSpeed += 2;//没下落50层,落脚点下落速度加快0.5.
- }
- if (sc == 70) {
- Global.instance.InitSpeed += 2;//每下落50层,player下落速度加0.25
- Global.instance.FHFallSpeed += 4;//没下落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<MM.my.rank){
- Global.instance.SetR(MM.my.rank);
- Global.instance.setRemoteScore(MM.other);
- }
- });
- });*/
- }
- }
|