-
Creator 版本:2.2
-
目标平台: pc
不好意思,麻烦各位大佬了,我之前从网上学了一段键盘移动的代码:
onLoad() {
this.left = false;
this.right = false;
this.up = false;
this.down = false;//初始化方向键
cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);//按键监听开启
},
start() {
},
onKeyDown(event) {
switch(event.keyCode) {
case cc.macro.KEY.d: this.right = true;
break;
case cc.macro.KEY.a: this.left = true;
break;
case cc.macro.KEY.w: this.up = true;
break;
case cc.macro.KEY.s: this.down = true;
break;//设定四个方向键
}
},
onKeyUp(event) {
switch(event.keyCode) {
case cc.macro.KEY.d: this.right = false;
break;
case cc.macro.KEY.a: this.left = false;
break;
case cc.macro.KEY.w: this.up = false;
break;
case cc.macro.KEY.s: this.down = false;
break;//停止按键
}
},
update(dt) {
if(this.right) {
this.node.scaleX = 1;//设定角色面向沿行进方向
this.node.x += this.speeddt;//计算行进距离
}else if(this.left) {
this.node.scaleX = -1;
this.node.x -= this.speeddt;
}if(this.up) {
this.node.y += this.speeddt;
}else if(this.down) {
this.node.y -= this.speeddt;
}
这段代码没问题,可以用。后来我做了个人物属性图显示界面(名字是playerPro),做成prefab,然后依样画葫芦的设置了键盘监听事件:
onLoad() {
cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);//按键监听开启
},
start () {
},
onKeyDown(event) {
switch(event.keyCode) {
case cc.macro.KEY.b: this.pro = true;//设定按键b
break;
}
},
onKeyUp(event) {
switch(event.keyCode) {
case cc.macro.KEY.b: this.pro = false;
}
},
update() {
var playerPro = cc.instantiate(this.playerPro);将预制的人物属性界面生成
if(this.pro) {
this.node.addChild(playerPro);
}
倒也可行,在游戏中按下b键确实跳出了人物属性界面playerPro,问题是,我想要再按一次b键就能关闭属性界面的功能,因为前面对这个键盘监听事件知其然不知其所以然,所以搞得现在不知道如何继续写代码,无从下手。
想请教这个if(this.pro)的意思,是不是就是if this.pro = true?
如果是这样,就表示,全部代码的意思是指令电脑,按下b键就跳出属性界面,那为什么必须配套监听松开按键的事件?我试过删除掉一半,只保留按下按键或只保留松开按键那部分代码,都不行。甚至更让我好奇的是,删除掉松开按键的代码部分更严重,总感觉好像是松开按键电脑才收到了加载指令。
之前还做了个行走动画,设定条件是按下行走键的同时播放行走动画,在行走代码那里加上几句,变成这样:
onLoad() {
this.speed = 200;
this.left = false;
this.right = false;
this.up = false;
this.down = false;//初始化方向键
cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);//按键监听开启
animation.on(‘Play’,this.onKeyUp,this);//开启动画监听
},
start() {
},
onKeyDown(event) {
switch(event.keyCode) {
case cc.macro.KEY.d: this.right = true;
break;
case cc.macro.KEY.a: this.left = true;
break;
case cc.macro.KEY.w: this.up = true;
break;
case cc.macro.KEY.s: this.down = true;
break;//设定四个方向键
}
},
onKeyUp(event) {
switch(event.keyCode) {
case cc.macro.KEY.d: this.right = false;
break;
case cc.macro.KEY.a: this.left = false;
break;
case cc.macro.KEY.w: this.up = false;
break;
case cc.macro.KEY.s: this.down = false;
break;//停止按键
}
},
update(dt) {
var animation = this.getComponent(cc.Animation); //获取动画组件
if(this.right) {
this.node.scaleX = 1;//设定角色面向沿行进方向
this.node.x += this.speeddt;//计算行进距离
}else if(this.left) {
this.node.scaleX = -1;
this.node.x -= this.speeddt;
}if(this.up) {
this.node.y += this.speeddt;
}else if(this.down) {
this.node.y -= this.speeddt;
animation.play(‘goDown’);//播放向下行走动画
}
这些代码怎么说呢,游戏中向下行走,确实会播放行走动画,但我要的是按下行走键的同时播放,松开按键停止播放,这样动画才会跟移动同步。但结果却是在松开按键的时候才播放动画,要是持续按着行走键,反而不会播放动画了。
加上上面说的那些,更让我怀疑了,这些键盘监听到底是怎么运作的,确定不是松开按键的时候才发出指令?

