本文来自http://blog.csdn.net/runaying ,引用必须注明出处!
cocos2d-x节点(CCNotificationCenter.h)API
温馨提醒:为了大家能更好学习,强烈推荐大家看看本人的这篇博客 Cocos2d-X权威指南笔记
通知中心管理通知
///cocos2d-x-3.0alpha0/cocos2dx/support
//通知中心管理通知
#ifndef __CCNOTIFICATIONCENTER_H__
#define __CCNOTIFICATIONCENTER_H__
#include "cocoa/CCObject.h"
#include "cocoa/CCArray.h"
NS_CC_BEGIN
class ScriptHandlerMgr;
class CC_DLL NotificationCenter : public Object
{
friend class ScriptHandlerMgr;
public:
/** NotificationCenter constructor //构造函数
* @js ctor
*/
NotificationCenter();
/** NotificationCenter destructor //析构函数
* @js NA
* @lua NA
*/
~NotificationCenter();
/** Gets the single instance of NotificationCenter. */
static NotificationCenter *getInstance();
/** Destroys the single instance of NotificationCenter. */
static void destroyInstance();
/** @过时不再需要建议使用新的 API ,可以使用 getInstance() 代替 */
CC_DEPRECATED_ATTRIBUTE static NotificationCenter *sharedNotificationCenter(void);
/** @过时不再需要建议使用新的 API ,可以使用 destroyInstance() 代替 */
CC_DEPRECATED_ATTRIBUTE static void purgeNotificationCenter(void);
/** @brief 为指定 target 添加一个 观察者
* @param target target 要遵守通知事件
* @param selector 指定通知事件发生时.将调用回调函数
* @param name The name of this notification.
* @param sender 通知对象其目标要接收,只有发件人发送的通知可以传递到目标。 NULL表示发送方不用传送通知到目标.
*/
void addObserver(Object *target,
SEL_CallFuncO selector,
const char *name,
Object *sender);
/** @brief 根据制定的 target name 删除 observer
* @param target The target of this notification.
* @param name The name of this notification.
*/
void removeObserver(Object *target,const char *name);
/** @brief 移除所有使用这个 target 注册的通知
* @param target The target of this notification.
* @returns the number of observers removed
*/
int removeAllObservers(Object *target);
/** @brief 注册脚本绑定一个 hander
* @note 仅支持Lua的绑定
* @param handler The lua handler.
*/
void registerScriptObserver(Object *target,int handler,const char* name);
/** 注销脚本 observer */
void unregisterScriptObserver(Object *target,const char* name);
/** @brief 使用 name Posts 一个通知事件
* @param name The name of this notification.
*/
void postNotification(const char *name);
/** @brief 使用 name Posts 一个通知事件
* @param name The name of this notification.
* @param sender posting 通知的对象,可以是 NULL
*/
void postNotification(const char *name, Object *sender);
/** @brief Gets script handler.
* @note Only supports Lua Binding now.
* @return The script handle.
*/
inline int getScriptHandler() const { return _scriptHandler; };
/** @brief Gets observer script handler.
* @param name The name of this notification.
* @return The observer script handle.
*/
int getObserverHandlerByName(const char* name);
private:
// 内部功能
// 检查是否存在制定 target 名字的 observer
bool observerExisted(Object *target,const char *name, Object *sender);
// variables
//
Array *_observers;
int _scriptHandler;
};
class CC_DLL NotificationObserver : public Object
{
public:
/** @brief NotificationObserver constructor
* @param target target 要遵守通知事件
* @param selector 指定通知事件发生时.将调用回调函数.
* @param name The name of this notification.
* @param sender 通知对象其目标要接收,只有发件人发送的通知可以传递到目标。 NULL表示发送方不用传送通知到目标
* @js NA
* @lua NA
*/
NotificationObserver(Object *target,
SEL_CallFuncO selector,
const char *name,
Object *sender);
/** NotificationObserver 析构函数
* @js NA
* @lua NA
*/
~NotificationObserver();
/** 调用的回调函数观察员
* @js NA
* @lua NA
*/
void performSelector(Object *sender);
// Getters / Setters
/**
* @js NA
* @lua NA
*/
Object* getTarget() const;
/**
* @js NA
* @lua NA
*/
SEL_CallFuncO getSelector() const;
/**
* @js NA
* @lua NA
*/
const char* getName() const;
/**
* @js NA
* @lua NA
*/
Object* getSender() const;
/**
* @js NA
* @lua NA
*/
int getHandler() const;
/**
* @js NA
* @lua NA
*/
void setHandler(int handler);
private:
Object* _target;
SEL_CallFuncO _selector;
std::string _name;
Object* _sender;
int _handler;
};
NS_CC_END
#endif//__CCNOTIFICATIONCENTER_H__