cocos2d js键盘按键相关

来源:互联网 发布:淘宝宝贝广告信息违规 编辑:程序博客网 时间:2024/06/08 14:44

几个问题,

1:按键处理中尽量用if else,不要用switch

                if(key == 65){                    that._isLeftPressed = false;                }else if(key == 68){                    that._isRightPressed = false;                }

2:按键处理中只负责改变物体的方向等等,物体的x y位置的变动、累加什么的不要放在按键处理中,应该放在update中

update:function(){        //左键按下、右键按下、抬起只能执行其一,所以左右连续快速移动时并不会执行抬起动作        if(this._isLeftPressed){//如果左键按下            if(this.getChildByTag(ArmatureTag) == null){                this.addChild(this._armature);            }            this._armature.scaleX = 1;            this._armature.x -= 1.5;            this._kalin.setVisible(false);        }else if(this._isRightPressed){//如果右键按下            if(this.getChildByTag(ArmatureTag) == null){                this.addChild(this._armature);            }            this._armature.scaleX = -1;            this._armature.x += 1.5;            this._kalin.setVisible(false);        }else{//左键或者右键抬起            if(this.getChildByTag(ArmatureTag)){                this.removeChildByTag(ArmatureTag);            }            this._kalin.setVisible(true);            this._kalin.x = this._armature.x;            this._kalin.scaleX = this._armature.scaleX;        }    }
3:参考第2条里update的内容,比如要实现反复按左右键取消中间的站立动作的功能,不要想得太复杂,直接第2条的update就能解决,因为最后才会判断抬起的逻辑,快速左右按方向键就可以实现左右连续的移动

0 0