首页 > 代码库 > cocos2d 事件响应

cocos2d 事件响应

1.首先定义唯一标识:

#define GAME_EVENT_PLAYER_EXP_UPDATED "player_exp_updated"

2.注册事件监听

cocos2d::EventListener* m_pPlayerExpListener;

 m_pPlayerExpListener = EventListenerCustom::create(GAME_EVENT_PLAYER_EXP_UPDATED, [this](EventCustom* pEvent)

                                                       {

                                                           //do something

                                                       });

 getEventDispatcher()->addEventListenerWithSceneGraphPriority(m_pPlayerExpListener, this);

 

3.触发事件

Director::getInstance()->getEventDispatcher()->dispatchCustomEvent(GAME_EVENT_PLAYER_EXP_UPDATED, NULL);