使用1.7cocos
show:function () {
this.node.setPosition(0, 0);
this.Register_keyboardEvent();
},
hide:function () {
this.node.x = 3000;
this.Unregister_keyboardEvent();
},
// use this for initialization
onLoad:function () {
// set initial move direction
//this.turnUp();
//this.Register_keyboardEvent();
},
onDestroy:function () {
this.Unregister_keyboardEvent();
},
//注册键盘事件
Register_keyboardEvent:function () {
cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
},
//卸载键盘事件
Unregister_keyboardEvent:function () {
cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
},
onKeyDown:function (event) {
switch(event.keyCode) {
case cc.KEY.up:
console.log('turn Up');
this.turnUp();
break;
case cc.KEY.down:
console.log('turn Down');
this.turnDown();
break;
case cc.KEY.a:
console.log('a key');
break;
case cc.KEY.b:
console.log('b key');
break;
}
},
第二次调用就无法使用键盘
