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学习笔记(七)组件我们初步的认识了组件,并学会了如何定义局部组件和全局组件,上一篇内容仅仅只是对组件一个简单的入门,并没有深入的了解组件当中的其它机制,本篇博客将会带大 ...
随机推荐
- SpringCloud 之 Fegin —— 发送GET、POST请求以及文件上传
由于项目需要调用其他微服务的数据,首先想到的就是写一个http网络请求的工具类,但是想到在之前看springCloud的时候里面有这个Fegin可以实现,就顺便实践一下,虽然过程有点坎坷,好在都顺利解 ...
- [Swift]LeetCode1064. 不动点 | Fixed Point
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- Python-控制流
if #!/usr/bin/python number=23 guess=int(input('Enter an interger:')) #input返回的结果是string类型,需要用int()转 ...
- 网络流模板(模板题:POJ1273)
模板题:POJ1273 EK: #include <queue> #include <cstdio> #include <cstring> #include < ...
- 【Oracle】truncate分区表
分区表是生产中常用的一种表,它可以实现数据的按类存放,极大的提高了数据的查询及维护.当我们不需要某一分区的数据时,可以采用truncate来清空分区.实验如下: SQL)) partition by ...
- Deutsch lernen (11)
1. anwesend a. 出席的,在场的 ~ abwesend Es waren gegen 50 Leute anwesend. 2. gespannt a. (心情)急切的,急于想知道的:紧张 ...
- Dll中的方法向外返回dynamic类型可能会失败
如果Dll中有某个类的方法返回dynamic实例,并且dynamic对象实际实例为匿名类类型,则Dll的外部使用者可能最终无法正常使用此dynamic对象.当使用此dynamic对象时,可能会遇到x属 ...
- Generics of a Higher Kind
http://adriaanm.github.io/files/higher.pdf https://www.atlassian.com/blog/archives/scala-types-of-a- ...
- Nginx服务器部署SSL证书手机不信任解决方法
在wosign申请证书并按指南正确部署证书后,如果发现PC浏览器访问正常,手机或safari浏览器提示证书不受信任,那肯定是在文件传输解压过程中导致证书文件中出现空格.乱码之类的情况,这里教您轻松四步 ...
- Selenium3+python自动化-iframe
一.frame和iframe区别 Frame与Iframe两者可以实现的功能基本相同,不过Iframe比Frame具有更多的灵活性. frame是整个页面的框架,iframe是内嵌的网页元素,也可以说 ...