多平台响应键盘事件!(适用于Cocos2dx 3.0 alpha以上版本)
2014-04-24 14:22
543 查看
Hello everyone! For a week I’ve been looking on how to make a keyboard work!
I managed to figure it out and I want to share my knowledge with you!
So, before we get started - this tutorial only works on Cocos2d-x 3.0alpha and later.
We will start by making two functions in the scene we want keyboard on.
They will be:
首先在需要键盘处理事件的场景中文件中添加如下两个函数。
These functions will be called when we press/release a key on the keyboard.
Next we need a listener to look for the keyboard we will create it like so (I did it in the init function)
当键盘按下时会调用上面这两的函数。
接下来在init()方法中添加如下代码来监听键盘事件。
This code creates a keyboard listener and then setting what functions will be called when the key is pressed or released.
Now Our program can detect keyboard! Wait… How do I know what key is pressed? It is simple! Let me show you:
然后就可以在最上面的两个方法中添加keyCode的判定了。
This piece of code will check what is the key-code of the key that was pressed. The list of key-codes is inside the EventKeyboard class. To use a keycode you just type:
EventKeyboard::KeyCode::KEY_**whatever key** - you will usually get a list of available keys to chose from.
Well, I think that’s it! Enjoy!
参考:http://www.cocos2d-x.org/forums/6/topics/39145
I managed to figure it out and I want to share my knowledge with you!
So, before we get started - this tutorial only works on Cocos2d-x 3.0alpha and later.
We will start by making two functions in the scene we want keyboard on.
They will be:
首先在需要键盘处理事件的场景中文件中添加如下两个函数。
OurScene.h: void keyPressed(cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event *event); void keyReleased(cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event *event); OurScene.cpp: void OurScene::keyPressed(cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event *event) { } void OurScene::keyReleased(cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event *event) { }
These functions will be called when we press/release a key on the keyboard.
Next we need a listener to look for the keyboard we will create it like so (I did it in the init function)
当键盘按下时会调用上面这两的函数。
接下来在init()方法中添加如下代码来监听键盘事件。
auto keyboardListener = EventListenerKeyboard::create(); keyboardListener->onKeyPressed = CC_CALLBACK_2(OurScene::keyPressed, this); keyboardListener->onKeyReleased = CC_CALLBACK_2(OurScene::keyReleased, this); EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this); // use if your version is below cocos2d-x 3.0alpha.1 // use this: Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this); if you are using cocos2d-x 3.0alpha.1 and later!
This code creates a keyboard listener and then setting what functions will be called when the key is pressed or released.
Now Our program can detect keyboard! Wait… How do I know what key is pressed? It is simple! Let me show you:
然后就可以在最上面的两个方法中添加keyCode的判定了。
//put this inside keyPressed or keyReleased if (keyCode == EventKeyboard::KeyCode::KEY_W) { CCLog("W key was pressed"); }
This piece of code will check what is the key-code of the key that was pressed. The list of key-codes is inside the EventKeyboard class. To use a keycode you just type:
EventKeyboard::KeyCode::KEY_**whatever key** - you will usually get a list of available keys to chose from.
Well, I think that’s it! Enjoy!
参考:http://www.cocos2d-x.org/forums/6/topics/39145
相关文章推荐
- 多平台响应键盘事件!(适用于Cocos2dx 3.0 alpha以上版本)
- 多平台响应键盘事件!(适用于Cocos2dx 3.0 alpha以上版本号)
- cocos2dx 3.0以上版本的触摸事件
- 图像处理之其他杂项(三)之cvSnakeImage改进升级兼容 适用于opencv2,,在opencv3.0以上版本中测试通过
- cocos2dx 响应windows键盘事件
- 详解cocos2dx 3.0的release版本在android平台的签名过程
- cocos2dx 弹出键盘无法响应键盘外的触摸事件
- 详解cocos2dx 3.0的release版本在android平台的签名过程
- Cocos2dx-3.0版本 从开发环境搭建(Win32)到项目移植Android平台过程详解
- cocos2dx 2.2.x版本和3.0以上版本创建项目
- Cocos2dx 3.0 以上版本 集成 MFC
- cocos2dx-3.0以上版本之 创建Sprite精灵的6种形式
- 百度地图3.0以上版本标注marker及其点击事件
- COCOS2DX3.0的3种触摸响应机制
- 隐藏自定义的tabbar之后,push到B视图,B视图的键盘工具条无法响应点击事件
- cocos2dx-3.0 移植android平台
- 低级键盘钩子,在WIN7以上版本的问题
- jq 3.0 以上版本不再支持$('#id').load(function() { });写法
- Android对触摸屏、键盘、轨迹球事件的响应过程