我们在学习一个新知识的时候,应该了解一些相关的知识,今天我们就来学习关于Cocos2d-x物理按键交互,希望在以后的学习中能用到。

bool HelloWorld::init()

{

//////////////////////////////

// 1. super init first

if ( !Layer::init() )

{

return false;

}

auto listener = EventListenerKeyboard::create();

listener->onKeyReleased = [](EventKeyboard::KeyCode code,Event *e){

CCLOG(“key code : %d”,code);

//按下对应键触发对应方法 

switch (code) {

case EventKeyboard::KeyCode::KEY_BACKSPACE:

Director::getInstance()->end();

break;

default:

break;

}

};

Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);

return true;