本文来自http://blog.youkuaiyun.com/runaying ,引用必须注明出处!
cocos2d-x节点(CCEventListenerCustom.h)API
温馨提醒:为了大家能更好学习,强烈推荐大家看看本人的这篇博客 Cocos2d-X权威指南笔记
自定义监听事件
///cocos2d/cocos2d-x-3.0alpha0/cocos2dx/event_dispatcher
//自定义监听事件
#ifndef __cocos2d_libs__CCCustomEventListener__
#define __cocos2d_libs__CCCustomEventListener__
#include "CCEventListener.h"
NS_CC_BEGIN
class EventCustom;
/**
* Usage: //用法
* auto dispatcher = EventDispatcher::getInstance();
* Adds a listener:
*
* auto callback = [](CustomEvent* event){ do_some_thing(); };
* auto listener = CustomEventListener::create(callback);
* dispatcher->addEventListenerWithSceneGraphPriority(listener, one_node);
*
* 调度一个自定义的事件
*
* Event event("your_event_type");
* dispatcher->dispatchEvent(&event);
*
* Removes a listener
*
* dispatcher->removeListener(listener);
*/
class EventListenerCustom : public EventListener
{
public:
/**使用 事件类型,和回调函数 创建一个监听器.
* @param eventType 事件类型.
* @param callback 指定事件发生时,创建回调函数
*/
static EventListenerCustom* create(const std::string& eventName, std::function<void(EventCustom*)> callback);
/// Overrides
virtual bool checkAvaiable() override;
virtual EventListenerCustom* clone() override;
protected:
/** 构造函数 */
EventListenerCustom();
/**使用 事件类型,和回调函数 创建一个监听器. */
bool init(const std::string& eventName, std::function<void(EventCustom*)> callback);
std::function<void(EventCustom*)> _onCustomEvent;
};
NS_CC_END
#endif /* defined(__cocos2d_libs__CCCustomEventListener__) */