Cocos Creator也没有报错提醒
cc.Class({
extends: cc.Component,
properties: {
jumpHeight: 0,
jumpDuration: 0,
maxMoveSpeed: 0,
accel: 0,
},
setJumpAction: function () {
var jumpUp = cc.moveBy(this.jumpDuration, cc.v2(0, this.jumpHeight)).easing(cc.easeCubicActionOut());
var jumpDown = cc.moveBy(this.jumpDuration, cc.v2(0, -this.jumpHeight)).easing(cc.easeCubicActionOut());
return cc.repeatForever(cc.sequence(jumpUp, jumpDown));
},
onKeyDown(event) {
switch (event.keyCode) {
case cc.macro.KEY.a:
this.accLeft = turn;
break;
case cc.macro.KEY.d:
this.accRight = turn;
break;
}
},
onKeyUp(event) {
switch (event.keyCode) {
case cc.macro.KEY.a:
this.accLeft = false;
break;
case cc.macro.KEY.b:
this.accRight = false;
break;
}
},
onLoad: function () {
this.jumpAction = this.setJumpAction();
this.node.runAction(this.jumpAction);
this.accLeft = false;
this.accRight = false;
this.xSpeed = 0;
cc.systemEvent.on(cc.systemEvent.EvevtType.KEY_DOWN, this.onKeyDown, this);
cc.systemEvent.on(cc.systemEvent.EvevtType.KEY_UP, this.onKeyUp, this);
},
onDestroy() {
cc.systemEvent.off(cc.systemEvent.EvevtType.KEY_DOWN, this.onKeyDown, this);
cc.systemEvent.off(cc.systemEvent.EvevtType.KEY_UP, this.onKeyUp, this);
},
start () {
},
update: function (dt) {
// 根据当前加速度方向每帧更新速度
if (this.accLeft) {
this.xSpeed -= this.accel * dt;
} else if (this.accRight) {
this.xSpeed += this.accel * dt;
}
// 限制主角的速度不能超过最大值
if (Math.abs(this.xSpeed) > this.maxMoveSpeed) {
// if speed reach limit, use max speed with current direction
this.xSpeed = this.maxMoveSpeed * this.xSpeed / Math.abs(this.xSpeed);
}
// 根据当前速度更新主角的位置
this.node.x += this.xSpeed * dt;
},
});