cocos2d-x CCNotificationCenter一个优化想法
2013-12-14 23:07
330 查看
void CCNotificationCenter::postNotification(const char *name, CCObject *object) { CCArray* ObserversCopy = CCArray::createWithCapacity(m_observers->count()); ObserversCopy->addObjectsFromArray(m_observers); CCObject* obj = NULL; CCARRAY_FOREACH(ObserversCopy, obj) { CCNotificationObserver* observer = (CCNotificationObserver*) obj; if (!observer) continue; if (!strcmp(name,observer->getName()) && (observer->getObject() == object || observer->getObject() == NULL || object == NULL)) observer->performSelector(object); } if (m_scriptHandler) { CCScriptEngineProtocol* engine = CCScriptEngineManager::sharedManager()->getScriptEngine(); engine->executeNotificationEvent(this, name); } }
从上面代码中可以看到发送消息的时候是遍历所有的Observer,这样对于频繁发送消息的时候,非常低效率。我的想法是修改CCNotificationCenter的 CCArray *m_observers 为 CCDictionary * m_observers(key为消息,value是一个CCSet *),因为CCNotificationCenter使用过程中更多是面向消息。
修改后的代码如下:
// // internal functions // bool CCNotificationCenter::observerExisted(CCObject *target,const char *name) { CCSet* mgsObservers = (CCSet*)m_observersDic->objectForKey(name); if (mgsObservers) { for (CCSetIterator iter = mgsObservers->begin(); iter != mgsObservers->end(); iter++ ){ CCNotificationObserver* observer = (CCNotificationObserver*) (* iter); if (observer->getTarget() == target) return true; } } return false; } // // observer functions // void CCNotificationCenter::addObserver(CCObject *target, SEL_CallFuncO selector, const char *name, CCObject *obj) { if (this->observerExisted(target, name)) return; CCNotificationObserver *observer = new CCNotificationObserver(target, selector, name, obj); observer->autorelease(); CCSet* mgsObservers = (CCSet*)m_observersDic->objectForKey(name); if(!mgsObservers){ mgsObservers = CCSet::create(); m_observersDic->setObject(mgsObservers, name); } mgsObservers->addObject(observer); } void CCNotificationCenter::removeObserver(CCObject *target,const char *name) { CCSet* mgsObservers = (CCSet*)m_observersDic->objectForKey(name); if(mgsObservers){ for (CCSetIterator iter = mgsObservers->begin(); iter != mgsObservers->end(); iter++ ){ CCNotificationObserver* observer = (CCNotificationObserver*) (* iter); if (observer->getTarget() == target){ mgsObservers->removeObject(observer); return; } } } } int CCNotificationCenter::removeAllObservers(CCObject *target) { int removeCount = 0; CCDictElement* pElement = NULL; CCDICT_FOREACH(m_observersDic, pElement) { CCSet* mgsObservers = (CCSet*)pElement->getObject(); for (CCSetIterator iter = mgsObservers->begin(); iter != mgsObservers->end();){ CCNotificationObserver* observer = (CCNotificationObserver*) (* iter); if (observer->getTarget() == target){ mgsObservers->removeObject(observer); removeCount ++ ; }else{ iter++ } } } return removeCount; } void CCNotificationCenter::postNotification(const char *name, CCObject *object) { CCSet* mgsObservers = (CCSet*)m_observersDic->objectForKey(name); if(mgsObservers){ for (CCSetIterator iter = mgsObservers->begin(); iter != mgsObservers->end(); iter++ ){ CCNotificationObserver* observer = (CCNotificationObserver*) (* iter); if ((observer->getObject() == object || observer->getObject() == NULL || object == NULL)){ observer->performSelector(object); } } } if (m_scriptHandler) { CCScriptEngineProtocol* engine = CCScriptEngineManager::sharedManager()->getScriptEngine(); engine->executeNotificationEvent(this, name); } } void CCNotificationCenter::postNotification(const char *name) { this->postNotification(name,NULL); }
本文原创,转载请注明原文地址!
相关文章推荐
- Cocos2d-x CCNotificationCenter 通知中心
- Cocos2D-x游戏开发之二十三:CCNotificationCenter观察者模式(2)-不同层之间事件的发送和接受
- cocos2d-x 血泪史(-1) CCNotificationCenter 与LUA
- Cocos2d-x 2.x编程之CCNotificationCenter
- Cocos2d-x CCNotificationCenter 通知中心 自定义消息事件
- Cocos2d-x CCNotificationCenter 通知中心
- Cocos2d-x v3.x 的事件派发机制(消息处理机制CCNotificationCenter)
- Cocos2d-x CCNotificationCenter
- cocos2d CCNotificationCenter
- Cocos2d-x CCNotificationCenter 通知中心
- cocos2d-x CCNotificationCenter 通知中心
- Cocos2D-x游戏开发之:CCNotificationCenter观察者模式基础
- Cocos2d-x CCNotificationCenter 通知中心
- Cocos2D-x游戏开发之二十二:CCNotificationCenter观察者模式基础(1)
- COCOS2D-X之CCNotificationCenter的简单使用Demo
- Cocos2d-x CCNotificationCenter 通知中心
- Cocos2d-x游戏开发中的消息机制:CCNotificationCenter的使用
- cocos2d-x节点(CCNotificationCenter.h)API
- Cocos2d-x 中 CCNotificationCenter 的使用
- cocos2d-JS 性能优化 - 对象缓冲池 ( cc.pool )