原谅我问一个很蠢的问题,我是新手

这是cocos creator官方的教程,一个吃星星的小游戏,我不懂的是在 setInputControl 里的 self.accLeft 是在哪里定义的?属性里面也没有啊,只是在onLoad里面定义了一下,为什么在这里这样写就能运行呢?

cc.Class({
extends: cc.Component,

properties: {
    // foo: {
    //     // ATTRIBUTES:
    //     default: null,        // The default value will be used only when the component attaching
    //                           // to a node for the first time
    //     type: cc.SpriteFrame, // optional, default is typeof default
    //     serializable: true,   // optional, default is true
    // },
    // bar: {
    //     get () {
    //         return this._bar;
    //     },
    //     set (value) {
    //         this._bar = value;
    //     }
    // },
    // 主角跳跃高度
    jumpHeight: 0,
    // 主角跳跃持续时间
    jumpDuration: 0,
    // 最大移动速度
    maxMoveSpeed: 0,
    // 加速度
    accel: 0,
    // 跳跃音效资源
    jumpAudio: {
        default: null,
        url: cc.AudioClip
    },
},

setJumpAction: function () {
    // 跳跃上升
    var jumpUp = cc.moveBy(this.jumpDuration, cc.p(0, this.jumpHeight)).easing(cc.easeCubicActionOut());
    // 下落
    var jumpDown = cc.moveBy(this.jumpDuration, cc.p(0, -this.jumpHeight)).easing(cc.easeCubicActionIn());
    // 添加一个回调函数,用于在动作结束时调用我们定义的其他方法
    var callback = cc.callFunc(this.playJumpSound, this);
    // 不断重复
    return cc.repeatForever(cc.sequence(jumpUp, jumpDown));
},

playJumpSound: function () {
    // 调用声音引擎播放声音
    cc.audioEngine.playEffect(this.jumpAudio, false);
},

setInputControl: function () {
    var self = this;
    // 添加键盘事件监听
    // 有按键按下时,判断是否是我们指定的方向控制键,并设置向对应方向加速
    cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, function (event){
        switch(event.keyCode) {
            case cc.KEY.a:
                self.accLeft = true;
                break;
            case cc.KEY.d:
                self.accRight = true;
                break;
        }
    });

    // 松开按键时,停止向该方向的加速
    cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, function (event){
        switch(event.keyCode) {
            case cc.KEY.a:
                self.accLeft = false;
                break;
            case cc.KEY.d:
                self.accRight = false;
                break;
        }
    });
},

// LIFE-CYCLE CALLBACKS:

onLoad: function () {
    // 初始化跳跃动作
    this.jumpAction = this.setJumpAction();
    this.node.runAction(this.jumpAction);
    
    // 加速度方向开关
    this.accLeft = false;
    this.accRight = false;
    // 主角当前水平方向速度
    this.xSpeed = 0;

    // 初始化键盘输入监听
    this.setInputControl();
},

start () {

},

update: function (dt) {
    // 根据当前加速度方向每帧更新速度
    if (this.accLeft) {
        this.xSpeed -= this.accel * dt;k
    } 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;
},

});

应该是在onload 里面直接创建了这个属性。js可以这么搞

没错!:joy: 我现在明白了

其实不在onload定义也能运行,不过还是建议onload定义一下

1赞