1.按键触发事件 EventListenerKeyboard:重写onKeyPressed和onKeyReleased;
实现如下:
auto keyListener = EventListenerKeyboard::create();
keyListener->onKeyPressed = [](EventKeyboard::KeyCode keyCode, Event* event){ log("Key %d pressed.", keyCode); };
keyListener->onKeyReleased = [](EventKeyboard::KeyCode keyCode, Event* event){ log("Key %d released.", keyCode); };
// 添加监听器
_eventDispatcher->addEventListenerWithSceneGraphPriority(keyListener, this);
2.自定义监听事件,方便与跨类调用
比如我在HelloWorld类中写
//1,创建监听器
EventListenerCustom* customListener = EventListenerCustom::create("ShowOtherNode",
CC_CALLBACK_1(HelloWorld::ShowOtherNode, this));
//2,注册监听器
getEventDispatcher()->addEventListenerWithSceneGraphPriority(customListener, this);
然后在自定义类中调用,我事先开启了调度器的update,在update中调用
//3,自定义事件触发条件,以下两种方法调用