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学习笔记(七)组件我们初步的认识了组件,并学会了如何定义局部组件和全局组件,上一篇内容仅仅只是对组件一个简单的入门,并没有深入的了解组件当中的其它机制,本篇博客将会带大 ...
随机推荐
- Redis(三)-Ubuntu下安装
Ubuntu 下安装 在 Ubuntu 系统安装 Redi 可以使用以下命令: $sudo apt-get update $sudo apt-get install redis-server 启动 R ...
- 好用的Cache辅助工具类
话不多说,直接上代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; usi ...
- android黑科技系列——Wireshark和Fiddler分析Android中的TLS协议包数据(附带案例样本)
一.前言 在之前一篇文章已经介绍了一款网络访问软件的破解教程,当时采用的突破口是应用程序本身的一个漏洞,就是没有关闭日志信息,我们通过抓取日志获取到关键信息来找到突破口进行破解的.那篇文章也说到了,如 ...
- android黑科技系列——分析某直播App的协议加密原理以及调用加密方法进行协议参数构造
一.前言 随着直播技术火爆之后,各家都出了直播app,早期直播app的各种请求协议的参数信息都没有做任何加密措施,但是慢慢的有人开始利用这个后门开始弄刷粉关注工具,可以让一个新生的小花旦分分钟变成网红 ...
- Spring boot -环境搭建 ,初步接触(1)
1. Eclipse 创建 maven project 项目目录如下: 2. pom.xml 配置文件 <project xmlns="http://maven.apache.or ...
- javascript中计算点击多少次
点击事件:onclick <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...
- selenium选错弹出层的下拉框
要先选中这个弹出层的form元素,再找下拉框 public void downSelectBox(){ driver.get("https://www.imooc.com/user/setp ...
- ROS:Nvidia Jetson TK1开发平台
原文链接: http://wiki.ros.org/NvidiaJetsonTK1 1. Nvidia Jetson TK1 Jetson TK1 comes pre-installed with L ...
- Currying vs Partial Application
柯里化相当于函数重构: 偏函数相当于函数适配. So, what is the difference between currying and partial application? As we s ...
- Math工具类的使用
1.包:java.lang 不需要导包 2.Math 类包含用于执行基本数学运算的方法,如初等指数.对数.平方根和三角函数. 特点: 该类中的方法都是静态方法,所以可以直接使用类名.方法名(实参)调 ...