cocos2dx中有6种监听器,他们都继承自EventListener类,分别是:
_listener = EventListenerTouchOneByOne::create();
_listener->setSwallowTouches(true);
_listener->onTouchBegan = std::bind(&MyTestScene::onTouchBegin,this,std::placeholders::_1,std::placeholders::_2);
_listener->onTouchEnded = std::bind(&MyTestScene::onTouchEnd,std::placeholders::_2);
然后要添加这个监听器:
eventDispatcher = Director::getInstance()->getEventDispatcher();
eventDispatcher->addEventListenerWithSceneGraPHPriority(_listener,this);
当不需要该监听器时应该移除:
_eventDispatcher->removeEventListener(listener);
自定义事件要使用EventListenerCustom来实现:
auto listener = EventListenerCustom::create("testEvent",[=](EventCustom* event)->void{
TestEventData *data = (TestEventData*)event->getUserData();
}
);
_eventDispatcher->addEventListenerWithFixedPriority(listener,1);
发送自定义事件:
TestEventData testData( str_mem );
EventCustom myEvent("testEvent");
myEvent.setUserData(&testData);
_eventDispatcher->dispatchEvent(&myEvent);