博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cocos2d-x-3.1 NotificationCenter (coco2d-x 学习笔记八)
阅读量:5342 次
发布时间:2019-06-15

本文共 3212 字,大约阅读时间需要 10 分钟。

在这里分享记录自己的学习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*/

转载于:https://www.cnblogs.com/yangykaifa/p/7099316.html

你可能感兴趣的文章
win8.1安装Python提示缺失api-ms-win-crt-runtime-l1-1-0.dll问题
查看>>
图片点击轮播(三)-----2017-04-05
查看>>
直播技术细节3
查看>>
《分布式服务架构:原理、设计于实战》总结
查看>>
java中new一个对象和对象=null有什么区别
查看>>
字母和数字键的键码值(keyCode)
查看>>
IE8调用window.open导出EXCEL文件题目
查看>>
Spring mvc初学
查看>>
有意思的代码片段
查看>>
C8051开发环境
查看>>
VTKMY 3.3 VS 2010 Configuration 配置
查看>>
01_1_准备ibatis环境
查看>>
windows中修改catalina.sh上传到linux执行报错This file is needed to run this program解决
查看>>
JavaScript中的BOM和DOM
查看>>
360浏览器兼容模式 不能$.post (不是a 连接 onclick的问题!!)
查看>>
spring注入Properties
查看>>
【BZOJ-2295】我爱你啊 暴力
查看>>
【BZOJ-1055】玩具取名 区间DP
查看>>
Bit Twiddling Hacks
查看>>
Windwos中的线程同步
查看>>