//人物跳跃的实现
cc.Class({
extends: cc.Component,
properties: {
jumpDuration:0,
jumpHeight:0,
},
onLoad () {
var isJump = false;
//注册按键时间
cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
//moveBy组合动画实现跳动
var jumUp = cc.moveBy(this.jumpDuration,cc.p(0,this.jumpHeight));
var jumDown = cc.moveBy(this.jumpDuration,cc.p(0,-this.jumpHeight));
// this.jumpB = cc.jumpBy(this.jumpDuration,cc.p(0,this.jumpHeight),100,1);
//callFunc返回一个Action
var setJump=cc.callFunc(function(){this.isJump=false},this,this);
this.act = cc.sequence(jumUp,jumDown,setJump);
},
//按键事件回调函数
onKeyDown(event){
let pos = this.node.y;
switch(event.keyCode) {
case cc.macro.KEY.j:
// this.node.runAction(this.act);
if (!this.isJump) {
this.isJump = true;
this.node.runAction(this.act);
};
console.log('posY = ' + this.node.y);
break;
}
}
});