cocosCreator 键盘四方向移动

onLoad: function () {
        this._playerPlayAction(2,"downStop",true)
        // 加速度方向开关
        this.accUpper = false;
        this.accDown = false;
        this.accLeft = false;
        this.accRight = false;
        // 主角当前各方向速度
        this.upperSpeed = 0;
        this.downSpeed = 0;
        this.leftSpeed = 0;
        this.rightSpeed = 0;
        //动作锁
        this.upperLick = true;
        this.downLick = true;
        this.leftLick = true;
        this.rightLick = true;
        // 初始化键盘输入监听
        this.setInputControl();
    },

    setInputControl: function () {
        var self = this;
        // 添加键盘事件监听
        cc.eventManager.addListener({
            event: cc.EventListener.KEYBOARD,
            // 有按键按下时,判断是否是我们指定的方向控制键,并设置向对应方向速度
            onKeyPressed: function(keyCode, event) {
                switch(keyCode) {
                    case cc.KEY.w:
                        self.upperSpeed = self.accel;
                        self.downSpeed = 0;
                        self.leftSpeed = 0;
                        self.rightSpeed = 0;
                        if(self.upperLick) self._playerPlayAction(1,"upperAnima",false)
                        break;
                    case cc.KEY.s:
                        self.downSpeed = -self.accel;
                        self.upperSpeed = 0;
                        self.leftSpeed = 0;
                        self.rightSpeed = 0;
                        if(self.downLick) self._playerPlayAction(2,"downAnima",false)
                        break;
                    case cc.KEY.a:
                        self.leftSpeed = -self.accel;
                        self.upperSpeed = 0
                        self.downSpeed = 0;
                        self.rightSpeed = 0;
                        if(self.leftLick) self._playerPlayAction(3,"levelAnima",false,true)
                        break;
                    case cc.KEY.d:
                        self.rightSpeed = self.accel;
                        self.upperSpeed = 0
                        self.downSpeed = 0;
                        self.leftSpeed = 0;
                        if(self.rightLick) self._playerPlayAction(4,"levelAnima",false)
                        break;
                }
            },
            // 松开按键时,停止向该方向的加速
            onKeyReleased: function(keyCode, event) {
                switch(keyCode) {
                    case cc.KEY.w:
                        self.upperLick = true;
                        if(self.upperSpeed!=0){
                            self.upperSpeed = 0;
                            self._playerPlayAction(1,"upperStop",true)
                        }
                        break;
                    case cc.KEY.s:
                        self.downLick = true;
                        if(self.downSpeed != 0){
                            self.downSpeed = 0;
                            self._playerPlayAction(2,"downStop",true)
                        }
                        break;
                    case cc.KEY.a:
                        self.leftLick = true;
                        if(self.leftSpeed != 0){
                            self.leftSpeed = 0;
                            self._playerPlayAction(3,"levelStop",true,true)
                        }
                        break;
                    case cc.KEY.d:
                        self.rightLick = true;
                        if(self.rightSpeed != 0){
                            self.rightSpeed = 0;
                            self._playerPlayAction(4,"levelStop",true)
                        }
                        break;
                }
            }
        }, self.node);
    },
    //移动动作
    _playerPlayAction:function (num,name, lick,turnRight) {
        var anim = this.node.getChildByName("anim").getComponent(cc.Animation)
        switch(num) {
            case 1:
                anim.play(name)
                this.upperLick = lick;
                this.downLick = true;
                this.leftLick = true;
                this.rightLick = true;
                break;
            case 2:
                anim.play(name)
                this.upperLick = true;
                this.downLick = lick;
                this.leftLick = true;
                this.rightLick = true;
                break;
            case 3:
                this.node.getChildByName("anim").scaleX = turnRight?1:-1;
                anim.play(name)
                this.upperLick = true;
                this.downLick = true;
                this.leftLick = lick;
                this.rightLick = true;
                break;
            case 4:
                this.node.getChildByName("anim").scaleX = turnRight?1:-1;
                anim.play(name)
                this.upperLick = true;
                this.downLick = true;
                this.leftLick = true;
                this.rightLick = lick;
                break;
        }
        if(name == 'playerRun' && anim.currentClip && anim.currentClip.name == 'playerRun'){
            return
        }       
    },
    // called every frame, uncomment this function to activate update callback
    update: function (dt) {
        // 根据当前速度更新主角的位置
        this.node.y += this.upperSpeed * dt;
        this.node.y += this.downSpeed * dt;
        this.node.x += this.leftSpeed * dt;
        this.node.x += this.rightSpeed * dt;
    },

你可能感兴趣的:(cocosCreator 键盘四方向移动)