大家好,我是一个Cocos Creator的小白,刚接触一个星期左右,目前在公司领导让做一个跑酷的游戏项目,现在遇到一个问题,无法实现蓄力跳跃,本人比较笨,直接贴出代码,请大神们帮我一下!万分感谢!
【这段代码是挂在场景的背景图片上,实现跳跃的方法】
cc.Class({
extends: cc.Component,
properties: {
Cat: {
default: null,
type: cc.Node,
}
},
onLoad: function () {
var self = this;
this.node.on('touchend', function (event) {
var visibleSize = cc.director.getVisibleSize();
if (event.getLocationX() < visibleSize.width / 2) {//如果不是在屏幕右侧点击屏幕的话
self.Cat.getComponent('Cat_Run').downRelease();//恢复跑步的状态
} else {
self.Cat.getComponent('Cat_Run').jump();//让Player跳跃
}
});
},
});
【这段代码是挂在玩家身上的】
cc.Class({
extends: cc.Component,
onCollisionEnter: function (other, self) {
//如果碰到的是障碍物
if (other.tag == 2 && self.tag == 1) {
cc.director.loadScene(‘Over’);//加载game over的场景
this.getComponent(cc.Animation).stop();//停止播放动画
}
},
properties: {
// Player跳跃高度
jumpHeight: 0,
// Player跳跃持续时间
jumpDuration: 0,
//Player状态
state: ‘Run’,
},
//Player跑
run: function () {
this.getComponent(cc.Animation).play(‘Cat_Run’);//播放Player跑步动画
this.state = ‘Run’;//设置Player的状态为跑步
},
//Player跳跃
jump: function () {
if (this.state == ‘Run’) {//如果Player的状态是跑步的话
this.state = ‘Jump’;//设置Player的状态为跳跃
this.getComponent(cc.Animation).stop();//停止播放跑步的动画
this.getComponent(cc.Animation).play(‘Cat_Jump’);//播放Player跳跃动画
this.node.runAction(cc.sequence(cc.jumpBy(this.jumpDuration, cc.p(0, 0), this.jumpHeight, 1),//跳跃持续时间和跳跃的次数
cc.callFunc(function () {
this.run();//Player继续跑步
}, this)));
}
},
start() {
var catCollider = cc.director.getCollisionManager();//获取碰撞检测系统
catCollider.enabled = true;//开启碰撞检测系统
// catCollider.enabledDebugDraw = true;//显示碰撞组件的碰撞检测范围
}
});



