在cocos2dx中封装好了观察者NotificationCenter,这个使用的非常广泛,在2.x中我已做描述,由于3.x的升级,用法有了轻微的改变,所以说一下:
注册观察者:
NotificationCenter::getInstance()->addObserver(this,
callfuncO_selector(HelloWorld::isSuccess), "signIn", NULL);
void HelloWorld::isSuccess(Ref*
sender){
auto msg = (__String*)sender;
CCLOG("msg=%s",msg->getCString());
}
设置事件发送者:
String *str = String::create("OK");
NotificationCenter::getInstance()->postNotification("signIn",
str);
注销观察者:
//注销全部
NotificationCenter::getInstance()->removeAllObservers(this);
//注销特定
NotificationCenter::getInstance()->removeObserver(this, "signIn");