I encounter a problem when I develop my game with cocos2d-x. After digging the issue for a while, I’m basically sure that this is a bug in cocos2d-x.
This is the function to add an observer,
void CCNotificationCenter::addObserver(CCObject *target, SEL_CallFuncO selector, const char *name, CCObject *obj);
The last parameter means which object the observer cares about. It shouldn’t be just ignored before post notification:
void CCNotificationCenter::postNotification(const char *name, CCObject *object)
CCObject* obj = NULL;
CCNotificationObserver* observer = (CCNotificationObserver*) obj;
The highlighted code is where the bug exists. It only checks the notification name, without the object that the observer cares about. There are 2 reasons:
# If we don’t check the object pointer, he observer~~>object doesn’t matter any more. There’s no different that we pass an object pointer or NULL as the last parameter when calling addObserver. It’s not it’s supposed to do. This also shows incomplete behavior comparing to NSNotificationCenter in Cocoa.
# There is a serious problem as a result. Consider more than one observer listens the same event of the same notification name. Each observer would receive the notification even if the event comes from an object they explicitly doesn’t care about. In this case, the postNotification would be a mess and take us a lot of time to debug!
The fix is very simple, just check the object before post notification:
if ) && == object))
After I add this code, my game works well.
If I misunderstood anything, please correct me.
BTW, do I have the an authority to commit this fix? Or should I ask any member of your develop team to commit it? Thanks.