多平台响应键盘事件!(适用于Cocos2dx 3.0 alpha以上版本)
Hello everyone! For a week I’ve been looking on how to make a keyboard work! 首先在需要键盘处理事件的场景中文件中添加如下两个函数。 These functions will be called when we press/release a key on the keyboard. 当键盘按下时会调用上面这两的函数。 接下来在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. 然后就可以在最上面的两个方法中添加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: Well,I think that’s it! Enjoy! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |