cocos2d-x-3.1 NotificationCenter (coco2d-x 学习笔记八)
在这里分享记录自己的学习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*/
cocos2d-x-3.1 NotificationCenter (coco2d-x 学习笔记八)的更多相关文章
- Learning ROS forRobotics Programming Second Edition学习笔记(八)indigo rviz gazebo
中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS forRobotics Pro ...
- python3.4学习笔记(八) Python第三方库安装与使用,包管理工具解惑
python3.4学习笔记(八) Python第三方库安装与使用,包管理工具解惑 许多人在安装Python第三方库的时候, 经常会为一个问题困扰:到底应该下载什么格式的文件?当我们点开下载页时, 一般 ...
- Go语言学习笔记八: 数组
Go语言学习笔记八: 数组 数组地球人都知道.所以只说说Go语言的特殊(奇葩)写法. 我一直在想一个人参与了两种语言的设计,但是最后两种语言的语法差异这么大.这是自己否定自己么,为什么不与之前统一一下 ...
- 【opencv学习笔记八】创建TrackBar轨迹条
createTrackbar这个函数我们以后会经常用到,它创建一个可以调整数值的轨迹条,并将轨迹条附加到指定的窗口上,使用起来很方便.首先大家要记住,它往往会和一个回调函数配合起来使用.先看下他的函数 ...
- go微服务框架kratos学习笔记八 (kratos的依赖注入)
目录 go微服务框架kratos学习笔记八(kratos的依赖注入) 什么是依赖注入 google wire kratos中的wire Providers injector(注入器) Binding ...
- Redis学习笔记八:集群模式
作者:Grey 原文地址:Redis学习笔记八:集群模式 前面提到的Redis学习笔记七:主从复制和哨兵只能解决Redis的单点压力大和单点故障问题,接下来要讲的Redis Cluster模式,主要是 ...
- Java IO学习笔记八:Netty入门
作者:Grey 原文地址:Java IO学习笔记八:Netty入门 多路复用多线程方式还是有点麻烦,Netty帮我们做了封装,大大简化了编码的复杂度,接下来熟悉一下netty的基本使用. Netty+ ...
- ROS学习笔记八:基于Qt搭建ROS开发环境
1 前言 本文介绍一种Qt下进行ROS开发的完美方案,使用的是ros-industrial的Levi-Armstrong在2015年12月开发的一个Qt插件ros_qtc_plugin,这个插件使得Q ...
- vue学习笔记(八)组件校验&通信
前言 在上一章博客的内容中vue学习笔记(七)组件我们初步的认识了组件,并学会了如何定义局部组件和全局组件,上一篇内容仅仅只是对组件一个简单的入门,并没有深入的了解组件当中的其它机制,本篇博客将会带大 ...
随机推荐
- lnmp 安装FTP服务 并配置FTP用户
lnmp 默认是不带FTP服务的,需要的童鞋要自行安装.步骤也很简单 一,进入lnmp目录,找到pureftpd.sh 二,直接运行该脚本 ./pureftpd.sh 按任意键开始安装,等待,安装成功 ...
- Spring Boot (7) JdbcTemplate访问数据库
使用jdbcTemplate操作数据库 spring framework对数据库的操作在jdbc上面做了深层次的封装,通过依赖注入功能,可以将datasource注册到jdbcTemplate中,学习 ...
- 复习HTML+CSS(7)
n HTML 注释 <--! 注释内容 --> 注意:注释内容不会显示,注释是为了将来维护方面. n 图片热点(图像地图) 图像热点:给一张图片加多个链接,默认情况下,一张图片只能加一 ...
- Spring JPA 简单配置使用
JPA 常用配置: # JPA (JpaBaseConfiguration, HibernateJpaAutoConfiguration) spring.data.jpa.repositories.b ...
- index seek和index scan 提高sql 效率
index seek和index scan 提高sql 效率解释解释index seek和index scan:索引是一颗B树,index seek是查找从B树的根节点开始,一级一级找到目标行.ind ...
- Deutsch lernen (06)
1. das Verzeichnis,-se 表格:名单,目录 Die Daten sind in einem Verzeichnis enthalten. (包括,含有) 2. enthalten ...
- Linux之tar.gz file
A tarball (tar.gz file) is compressed tar archive. The tar program provides the ability to create ta ...
- 参加EMCL感想
ECML,全名为欧洲机器学习会议,European Conference on Machine Learning 原文链接:http://blog.sina.com.cn/s/blog_59388e2 ...
- Dispatch Queues and Thread Safety
Dispatch Queues and Thread Safety It might seem odd to talk about thread safety in the context of di ...
- eas之缓存清理
apusic缓存清理,安装web框架补丁后,先清空apusic缓存,然后再重启服务apusic-domains-server1-deploy-easweb-tmpfiles