监听键盘响应事件
初始化并绑定
auto listener= EventListenerKeyboard::create();
listener->onKeyPressed= CC_CALLBACK_2(KeyboardTest::onKeyPressed,this);
listener->onKeyReleased= CC_CALLBACK_2(KeyboardTest::onKeyReleased,this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener,this);
函数原型
voidKeyboardTest::onKeyPressed(EventKeyboard::KeyCodekeyCode,Event* event)
{
}
voidKeyboardTest::onKeyReleased(EventKeyboard::KeyCodekeyCode,Event* event)
{
初始化并绑定
auto listener= EventListenerKeyboard::create();
listener->onKeyPressed= CC_CALLBACK_2(KeyboardTest::onKeyPressed,this);
listener->onKeyReleased= CC_CALLBACK_2(KeyboardTest::onKeyReleased,this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener,this);
函数原型
voidKeyboardTest::onKeyPressed(EventKeyboard::KeyCodekeyCode,Event* event)
{
}
voidKeyboardTest::onKeyReleased(EventKeyboard::KeyCodekeyCode,Event* event)
{
}
自定义事件
监听事件
_listener =EventListenerCustom::create("eventName",[=](EventCustom*event){
//fun();
});
_eventDispatcher->addEventListenerWithFixedPriority(_listener,1);
触发事件
EventCustom _event("eventName");
_event.setUserData();
_eventDispatcher->dispatchEvent(&event);