Cocos Creator-js跑酷游戏蓄力跳跃问题!!!

大家好,我是一个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;//显示碰撞组件的碰撞检测范围
}
});

1赞

各位大神都不在么

你的蓄力跳是想怎么样,按的越久跳的越高么

1赞

是的是的

你蓄力时候,在update里面更新跳跃高度,你放手的时候,就得到蓄力后的高度

1赞

能受累列出代码来么?我不会写。。。万分感谢!

在蓄力键上添加TOUCH_START和TOUCH_END监听蓄力开始事件和蓄力结束事件,其实就是点击开始和结束点击,设一个值,如is_gather_strength表示是否开始蓄力,在初始化的时候为false, 设置一个值,如jumpheight表示蓄力跳跃高度,初始化的时候为0

-----蓄力开始事件方法里面-----
jumpheight = 0;
is_gather_strength = true;
-----蓄力开始事件方法里面-----

-----蓄力结束事件方法里面-----
is_gather_strength = false;

如果有跳跃高度限制
if (jumpheight > 1000) {
jumpheight = 1000;
}
执行跳跃
-----蓄力结束事件方法里面-----

-----update里面-----
判断是否开始蓄力
if (is_gather_strength) {
jumpheight += dt * 500;
}
-----update里面-----

1赞

大神!感谢您的细心指导!但是按照您说的写了一下,运行不了呢,是我哪里写错了么?
cc.Class({
extends: cc.Component,

properties: {
    Cat: {
        default: null,
        type: cc.Node,
    }
},

onLoad: function () {
    var self = this;
    this.node.on('touchstart', function (event) {
        jumpheight = 0;
        is_gather_strength = true;
    });

    this.node.no('touchend', function (event) {
        is_gather_strength = false;
        if (jumpheight > 1000) {
            jumpheight = 1000;
        }
    });
},
update(dt) {
    if (is_gather_strength) {
        jumpheight += dt * 500;
    }
}

});

我只是说了下思路,你不能直接这样写啊,你的jumpheight和is_gather_strength是undefined的,你要声明一下,而且调用的时候加上this

1赞

:sob::sob::sob:还是运行不了。。。大神可以帮我写一个完整版么…论坛里都没有相关的方法:cry:
cc.Class({
extends: cc.Component,

properties: {
    Cat: {
        default: null,
        type: cc.Node,
        jumpheight = 0,//声明跳跃高度(Cat_Run脚本中也有这个变量,不知道是否会冲突)
        is_gather_strength = false,
    }
},

onLoad: function () {
    var self = this;
    this.node.on('touchstart', function (event) {
        jumpheight = 0;
        is_gather_strength = true;
        self.Cat.getComponent('Cat_Run').jump();//让Cat跳跃
    });

    var self = this;
    this.node.no('touchend', function (event) {
        is_gather_strength = false;
        self.Cat.getComponent('Cat_Run').jump();//让Cat跳跃
        if (jumpheight > 1000) {
            jumpheight = 1000;

        }
    });
},
update(dt) {
    if (is_gather_strength) {
        jumpheight += dt * 500;
    }
}

});

cc.Class({
extends: cc.Component,
properties: {

Cat: {
    default: null,
    type: cc.Node 
},
jumpheight:0,
is_gather_strength:false

},

onLoad: function () {

this.node.on('touchstart', function (event) {
    this.jumpheight = 0;
    this.is_gather_strength = true;
    this.Cat.getComponent('Cat_Run').jump();//让Cat跳跃
}, this); 

this.node.on('touchend', function (event) {
    this.is_gather_strength = false;
    this.Cat.getComponent('Cat_Run').jump();//让Cat跳跃
    if (this.jumpheight > 1000) {
        this.jumpheight = 1000;
    }
}, this);

},
update(dt) {

if (this.is_gather_strength) {
     this.jumpheight += dt * 500;
}

}
});

1赞

我也是新手,也想做个这个功能的游戏,我发现的是按下时间如果绑定在玩家上的话好像只有按在玩家上才有效,所以我是绑定在game上的。

我可以和你一起做么?不过我是业余选手

你绑在玩家上,当然只能点击玩家才有效,现在是点击按钮,那就绑在按钮上

1赞

做东西之前,先看先关文档和语法,后面踩的”坑”会少一些

的确,我也踩了很多坑,看文档才知道

1赞

如果只是想做个游戏就开干,很“苦”的…

大神!我按照你的知道写了一下,但是依然无法运行(在向您请教前(只能单纯的跳跃时)是可以运行),您看是不是因为jumpheight: 0,这个变量定义重复的原因啊?因为在Cat_Run脚本中也有这个定义:sweat::sweat::sweat:

我也看了几天的文档,但是不知道如何写…写完了总是无法运行:disappointed_relieved:

哪里报错了,把报错的地方截下图看下

1赞