在这里分享记录自己的学习NotificationCenter时候的代码,这里用NotificationManager进行管理使用NotificationCenter。

NotificationManager.cpp

#include "NotificationManager.h"
#include "VisibleRect.h" #define MSG_SWITCH_STATE "SwitchState" USING_NS_CC; enum
{
kSpriteTag
}; void runManager(){ auto mSene = Scene::create(); auto mLayer = NotificationManager::create(); mSene->addChild(mLayer); Director::getInstance()->runWithScene(mSene); } bool NotificationManager::init(){ if (!Layer::init())
{
return false;
} auto sp = Sprite::create("Images/t1.png");
sp->setPosition(VisibleRect::center());
addChild(sp, 1, kSpriteTag); //首先创建一个关闭键
auto mCloseItem = MenuItemFont::create("Close", CC_CALLBACK_1(NotificationManager::closeManager, this));
//设置关闭键相对坐标
mCloseItem->setPosition(VisibleRect::rightBottom().x - mCloseItem->getContentSize().width, VisibleRect::rightBottom().y + mCloseItem->getContentSize().height);
auto mCloseMenu = Menu::create(mCloseItem, 0);
//设置Menu从坐标系的(0,0)处開始
mCloseMenu->setPosition(Vec2::ZERO);
addChild(mCloseMenu); /*创建一个可开关的Menu*/
auto label1 = LabelTTF::create("switch off", "fonts/Marker Felt.ttf", 26);
auto label2 = LabelTTF::create("switch on", "fonts/Marker Felt.ttf", 26);
auto mSwitchItem1 = MenuItemLabel::create(label1);
auto mSwitchItem2 = MenuItemLabel::create(label2);
auto mSwitchToggle = MenuItemToggle::createWithCallback(CC_CALLBACK_1(NotificationManager::switchToggle, this), mSwitchItem1, mSwitchItem2, NULL);
mSwitchToggle->setSelectedIndex(1); //switch on选中显示
auto mSwitchMenu = Menu::create(mSwitchToggle, NULL);
mSwitchMenu->setPosition(Vec2(VisibleRect::bottom().x, VisibleRect::bottom().y + 100));
addChild(mSwitchMenu); setIsConnectToggle(true); //进行监听 return true;
} void NotificationManager::switchToggle(Ref* sender){
auto obj = (MenuItemToggle*)sender; auto sp1 = (Sprite*)getChildByTag(kSpriteTag); if (obj->getSelectedIndex()){ //switch on
sp1->setOpacity(255); //透明度100%
}
else{ //switch off
sp1->setOpacity(127); //半透明
} } void NotificationManager::setIsConnectToggle(bool b){
if (b){
NotificationCenter::getInstance()->addObserver(this, callfuncO_selector(NotificationManager::switchToggle), MSG_SWITCH_STATE,NULL);
}
else{
NotificationCenter::getInstance()->removeObserver(this, MSG_SWITCH_STATE);
}
} NotificationManager::~NotificationManager(){
NotificationCenter::getInstance()->removeObserver(this, MSG_SWITCH_STATE);
} //退出
void NotificationManager::closeManager(Ref *sender){
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.", "Alert");
return;
#endif Director::getInstance()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}

NotificationManager.h

#ifndef NOTIFICATIONMANAGER_H
#define NOTIFICATIONMANAGER_H
#include "cocos2d.h" class NotificationManager :public cocos2d::Layer
{
public:
virtual ~NotificationManager(); virtual bool init(); void closeManager(cocos2d::Ref *sender); void switchToggle(cocos2d::Ref *sender); void setIsConnectToggle(bool b); CREATE_FUNC(NotificationManager); //创建一个自己主动释放对象
}; void runManager(); #endif /*NOTIFICATIONMANAGER_H*/

最新文章

  1. Hibernate3.3.2 手动配置annotation环境
  2. div 自动满屏
  3. JS事件学习笔记(思维导图)
  4. webGL 学习教程
  5. DB2 SQL 递归实现多行合并
  6. git 回退和删除操作
  7. Maximum & Minimum Depth of Binary Tree
  8. Spring boot jar 后台运行
  9. js原生轮播图
  10. 数据库插入数据失败,log提示不能将值 NULL 插入列 'id'
  11. Java进阶(三十八)快速排序
  12. building tool的简单了解
  13. Web从入门到放弃<7>
  14. Go strings.Builder
  15. cetus系列~ 读写分离具体分析
  16. 关于List、Map循环时,进行删除的结论
  17. ES6学习笔记(二):引用数据类型
  18. 详说 Block Formatting Contexts (块级格式化上下文)
  19. 如何使用Git上传项目代码到github
  20. servlet转发与重定向

热门文章

  1. 线性预测与Levinson-Durbin算法实现
  2. 阿里云 CentOS 6.5 使用XAMPP 搭建LAMP环境
  3. Jquery中绑定事件的异同
  4. PHP序列化 反序列化
  5. 自学Python八 爬虫大坑之网页乱码
  6. SLAM: SLAM的发展历程(WIKI)
  7. Laravel -- windows apache .htaccess https 路由重写
  8. Day9 面向对象进阶
  9. xpath定位实列
  10. 怎么获取自定义核算项目里某一个类型的数据:做f7