NotificationCenter的基本使用:</span>
init方法中:</span>
NotificationCenter::getInstance()->addObserver(this,callfuncO_selector(HelloWorld::call2),"abc",nullptr); //添加观察对象 auto b1 = MenuItemImage::create("CloseNormal.png","CloseNormal.png",CC_CALLBACK_0(HelloWorld::call,this)); //按钮 auto menu = Menu::create(b1,nullptr); this->addChild(menu);
对应的方法:</span>
void HelloWorld::call() { log("call()"); NotificationCenter::getInstance()->postNotification("abc",this); } void HelloWorld::call2(Ref* obj) { log("call2()"); auto s = Sprite::create("CloseNormal.png"); s->setPosition(Vec2(100,100)); this->addChild(s); }
</pre><pre name="code" class="cpp">最后:
HelloWorld::~HelloWorld() { <span style="white-space:pre"> </span>NotificationCenter::getInstance()->removeObserver(this,"abc"); //取消监听 <span style="white-space:pre"> </span>//NotificationCenter::getInstance()->removeAllObservers(this); }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。