onEnter: function () {
this._super();
cc.log("HelloWorld onEnter");
cc.eventManager.addListener({ ①
event: cc.EventListener.KEYBOARD, ②
onKeyPressed: function(keyCode, event){ ③
cc.log("Key with keycode " + keyCode + " pressed");
},
onKeyReleased: function(keyCode, event){ ④
cc.log("Key with keycode " + keyCode + " released");
}
}, this);
},
onExit: function () {
this._super();
cc.log("HelloWorld onExit");
cc.eventManager.removeListeners(cc.EventListener.KEYBOARD); ⑤
}上述代码第①行cc.eventManager.addListener是通过快捷方式注册事件监听器对象。第②行代码是设置键盘事件cc.EventListener.KEYBOARD。第③行代码是设置键盘按下属性onKeyPressed,其中的参数keyCode是按下的键编号。第④行代码是设置键盘抬起属性onKeyReleased。JS: Key with keycode 124 released JS: Key with keycode 124 pressed JS: Key with keycode 139 pressed JS: Key with keycode 139 released JS: Key with keycode 124 released JS: Key with keycode 139 pressed JS: Key with keycode 124 pressed JS: Key with keycode 139 released JS: Key with keycode 124 released JS: Key with keycode 139 pressed JS: Key with keycode 124 pressed JS: Key with keycode 139 released JS: Key with keycode 124 released
《Cocos2d-x实战 JS卷》现已上线,各大商店均已开售:
京东:http://item.
原文:http://blog.csdn.net/tonny_guan/article/details/44810903