cocos2d-x节点(CCNotificationCenter.h)API

本文来自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__


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值