var gameConfig = require("../GameConfig.js"); var playerConfig = require("../PlayerConfig.js"); cc.Class({ extends: cc.Component, properties: { zOrder: { // ATTRIBUTES: default: 0, }, playerStates: { default: null, type: cc.Node, serializable: true, }, }, onLoad () { this.init(); this.bJump = false; this.handrailEndX = 0; this.jumpSpeedX = 0; this.jumpDuration = 1; }, init() { this.playerController = this.node.parent; //pConSt this.pConSt = this.playerController.getComponent('BasePlayerController'); //gStatesSt this.gStatesSt = this.pConSt.gStatesSt; //pStatesSt this.pStatesSt = this.playerStates.getComponent('BasePlayerStates'); this.spine = this.node.getComponent(sp.Skeleton); }, run(animName,callback) { this.setAnim(animName,true,callback); this.pStatesSt.currentState = this.pStatesSt.playerAnimState.run; }, /** * @param hurdlingTag 0不在跨栏区域,1在跨栏区域 */ jump(animName,callback,hurdlingTag) { //because action shcedule timer is not excellent so need to use update time to make animation for networking let hurdrailStartPX = this.pStatesSt.handrailArr[this.pStatesSt.passedHurdrailNum].startPX; this.handrailEndX = hurdrailStartPX+gameConfig.handrailLength; // this.jumpSpeedX = (this.handrailEndX+this.pStatesSt.handrailExtlen - this.playerController.x)*0.001/this.jumpDuration; // this.bJump = true; // jumpBy // cc.jumpBy(time,cc.v2(x,y),height,count) // cc.jumpBy(时间,停止跳跃时的坐标:原坐标 + (x,y)的值,跳跃高度,跳跃次数) let tw = cc.tween(this.playerController); if(hurdlingTag ==0){ tw.then(cc.jumpTo(0.5, cc.v2(this.pConSt.node.x+200, this.playerController.y), 0, 1)); }else{ tw.then(cc.jumpTo(0.5, cc.v2(this.handrailEndX+200, this.playerController.y), 0, 1)); } tw.call(function() { this.run('Run1'); if(!callback) return; callback(); }.bind(this)); tw.start(); this.setAnim(animName,false,function () {}.bind(this)); this.playJumpEffect(); }, playJumpEffect(){}, stagger(callback) { this.pStatesSt.currentSpeed = playerConfig.playerSpeedGrade.slow; this.pStatesSt.currentState = this.pStatesSt.playerAnimState.jump; this.setAnim('hurdling_2',false,function () { this.run('Run1'); if(!callback) return; callback(); }.bind(this)); }, setAnim(animName,bLoop,callback) { let track = this.spine.setAnimation(0, animName, bLoop); if (track) { // 注册动画的结束回调 this.spine.setCompleteListener((trackEntry, loopCount) => { let name = trackEntry.animation ? trackEntry.animation.name : ''; if (name === animName && callback) { callback(); } }); } }, update (dt) { if(this.bJump) { //count time to 0 second this.jumpDuration-=dt; //跑 this.playerController.x += this.jumpSpeedX*dt/0.001; if(this.jumpDuration<=0) { this.bJump = false; this.handrailEndX = 0; this.jumpSpeedX = 0; this.jumpDuration = 1; } } } });