观察者模式

来源:互联网 发布:nodejs 返回json对象 编辑:程序博客网 时间:2024/06/09 15:35
首先,要在监听模块中添加监听事件,即在onEnter()函数中
CCNotificationCenter::sharedNotificaitonCenter()->addObserver(CCObject *target,
                     SEL_CallFuncO selector,
                     const char *name,
                     CCObject *obj)
target为当前类的this指针,selector为事件触发时执行的函数,name是事件的名字,obj为传输的数据
同理在onExit()中要移除监听
CCNotificationCenter::sharedNotificaitonCenter()->removeObserver(CCObject *target,const char *name)
另外在条件触发处加上一句推送事件的通知
CCNotificationCenter::sharedNotificaitonCenter()->postNotification(const char *name, CCObject *object)
如此而已
网络模块也可以参照这个模式进行处理