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学习笔记(七)组件我们初步的认识了组件,并学会了如何定义局部组件和全局组件,上一篇内容仅仅只是对组件一个简单的入门,并没有深入的了解组件当中的其它机制,本篇博客将会带大 ...
随机推荐
- EasyUI DataGrid组织事件冒泡
在事件内部需要阻止的地方添加如下代码 ]; 解释: arguments.callee是获得我自定义的事件处理方法OnSelected的方法体. .caller是获得调用OnSelected的上层方法( ...
- POJ 1149 PIGS (AC这道题很不容易啊)网络流
PIGS Description Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlo ...
- NOI2007项链工厂——sbTreap代码
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> ...
- 2015 多校赛 第四场 1009 (hdu 5335)
Problem Description In an n∗m maze, the right-bottom corner is the exit (position (n,m) is the exit) ...
- sleep()和wait()的区别
1 sleep()方法,我们首先要知道该方法是属于Thread类中的.而wait()方法,则是属于Object类中的. 2 Thread.sleep和Object.wait都会暂停当前的线程,对于CP ...
- 设置Hadoop的 dataNode的单个Map的内存配置
1.进入hadoop的配置目录 ,找到 环境变量的 $HADOOP_HOME cd $HADOOP_HOME 2.修改dataNode 节点的 单个map的能使用的内存配置 找到配置的文件: /opt ...
- logical vs physical address
Logical vs physical address 1) An address generated by the CPU is a logical address. Whereas, an ad ...
- 在安卓JNI/NDK中使用C++11
在VS下编写的程序移植到Eclipse下,出现了问题: this file requires compiler and library support for iso C++ 2011 standar ...
- webpack学习(六)—webpack+react+es6(第3篇)
接上篇 : webpack学习(六)—webpack+react+es6(第2篇) 上篇其实是有问题的,问题在取服务器数据这块.this.props 表示那些一旦定义,就不再改变的特性,而 this. ...
- Microsoft Visual Studio 常用快捷键总结
table tr:nth-child(odd){ background: #FFFFCC; } table tr:nth-child(even){ background: #FFFF99; } Mic ...