CCNotificationCenter(二)---NotificationCenterTest
//类的定义 #ifndef __NOTIFICATIONCENTERTEST_H__
#define __NOTIFICATIONCENTERTEST_H__ #include "cocos2d.h" class NotificationCenterTest : public cocos2d::CCLayer
{
public:
NotificationCenterTest();
void toExtensionsMainLayer(cocos2d::CCObject* sender);
void toggleSwitch(cocos2d::CCObject *sender);
void connectToSwitch(cocos2d::CCObject *sender);
private:
bool m_bShowImage;
}; void runNotificationCenterTest(); #endif /* __NOTIFICATIONCENTERTEST_H__ */
//将当前层替换成活动层
void runNotificationCenterTest()
{
CCScene* pScene = CCScene::create();
NotificationCenterTest* pLayer = new NotificationCenterTest();
pScene->addChild(pLayer);
CCDirector::sharedDirector()->replaceScene(pScene);
pLayer->release();
}
//继承精灵类,写一个符合需求的精灵类
class Light : public CCSprite
{
public:
Light();
~Light();
//静态方法,初始化精灵
static Light* lightWithFile(const char* name);
//设置开还是关
void setIsConnectToSwitch(bool bConnectToSwitch);
//改变obj的状态
void switchStateChanged(CCObject* obj);
//改变图片的亮度
void updateLightState(); private:
//是否是连接状态
bool m_bConnected;
//是否是开启状态
static bool s_bSwitchOn;
};
//析构的时候要释放掉,否则会造成内存泄露
Light::~Light()
{
CCNotificationCenter::sharedNotificationCenter()->removeObserver(this, MSG_SWITCH_STATE);
}
//参数是图片名
Light* Light::lightWithFile(const char* name)
{
Light* pLight = new Light();
pLight->initWithFile(name);
pLight->autorelease();
return pLight;
}
NotificationCenterTest::NotificationCenterTest()
: m_bShowImage(false)
{
CCSize s = CCDirector::sharedDirector()->getWinSize();
//创建返回按钮
CCMenuItemFont* pBackItem = CCMenuItemFont::create("Back", this,
menu_selector(NotificationCenterTest::toExtensionsMainLayer));
pBackItem->setPosition(ccp(VisibleRect::rightBottom().x - , VisibleRect::rightBottom().y + ));
CCMenu* pBackMenu = CCMenu::create(pBackItem, NULL);
pBackMenu->setPosition( CCPointZero );
addChild(pBackMenu);
//创建总体开关按钮
CCLabelTTF *label1 = CCLabelTTF::create("switch off", "Marker Felt", );
CCLabelTTF *label2 = CCLabelTTF::create("switch on", "Marker Felt", );
CCMenuItemLabel *item1 = CCMenuItemLabel::create(label1);
CCMenuItemLabel *item2 = CCMenuItemLabel::create(label2);
CCMenuItemToggle *item = CCMenuItemToggle::createWithTarget(this, menu_selector(NotificationCenterTest::toggleSwitch), item1, item2, NULL);
// turn on
item->setSelectedIndex();//默认选中的是第二个按钮选项
CCMenu *menu = CCMenu::create(item, NULL);
menu->setPosition(ccp(s.width/+, s.height/));
addChild(menu); CCMenu *menuConnect = CCMenu::create();
menuConnect->setPosition(CCPointZero);
addChild(menuConnect);
//创建三个按钮
for (int i = ; i <= ; i++)
{
//创建精灵
Light* light = Light::lightWithFile("Images/Pea.png");
light->setTag(kTagLight+i);
light->setPosition(ccp(, s.height/*i));
addChild(light);
//创建按钮点击的切换选项
CCLabelTTF *label1 = CCLabelTTF::create("not connected", "Marker Felt", );
CCLabelTTF *label2 = CCLabelTTF::create("connected", "Marker Felt", );
CCMenuItemLabel *item1 = CCMenuItemLabel::create(label1);
CCMenuItemLabel *item2 = CCMenuItemLabel::create(label2);
CCMenuItemToggle *item = CCMenuItemToggle::createWithTarget(this, menu_selector(NotificationCenterTest::connectToSwitch), item1, item2, NULL);
item->setTag(kTagConnect+i);
item->setPosition(ccp(light->getPosition().x, light->getPosition().y+));
//加到菜单
menuConnect->addChild(item, );
if (i == )
{
//最后一个菜单项默认是显示第二种
item->setSelectedIndex();
}
bool bConnected = item->getSelectedIndex() == ? true : false;
//为真的话,就监听消息,否则释放消息
light->setIsConnectToSwitch(bConnected);
}
//发送MSG_SWITCH_STATE消息
CCNotificationCenter::sharedNotificationCenter()->postNotification(MSG_SWITCH_STATE, (CCObject*)item->getSelectedIndex());
}
void Light::setIsConnectToSwitch(bool bConnectToSwitch)
{
m_bConnected = bConnectToSwitch;
if (m_bConnected)
{
//设置MSG_SWITCH_STATE的监听
CCNotificationCenter::sharedNotificationCenter()->addObserver(this, callfuncO_selector(Light::switchStateChanged), MSG_SWITCH_STATE, NULL);
}
else
{
CCNotificationCenter::sharedNotificationCenter()->removeObserver(this, MSG_SWITCH_STATE);
}
updateLightState();
}
//监听接收到消息时所触发的函数
void Light::switchStateChanged(CCObject* obj)
{
//判断参数是否为空,空就为false,否则为true
s_bSwitchOn = obj == 0x00 ? false : true;
//改变精灵的状体
updateLightState();
}
//改变精灵的状体
void Light::updateLightState()
{
//如果开关的开着的,按钮也是出于连接状体,则设置透明度为255,值越大
//透明度越低,图像越明显
if (s_bSwitchOn && m_bConnected)
{
//this指向的是个精灵类,所以这里是直接对精灵进行操作的
this->setOpacity();
}
else
{
this->setOpacity();
}
}
//返回主菜单
void NotificationCenterTest::toExtensionsMainLayer(cocos2d::CCObject* sender)
{
ExtensionsTestScene* pScene = new ExtensionsTestScene();
pScene->runThisTest();
pScene->release();
}
//点击是否连接按钮
void NotificationCenterTest::toggleSwitch(CCObject *sender)
{
CCMenuItemToggle* item = (CCMenuItemToggle*)sender;
//获取当前按钮的选择项
int index = item->getSelectedIndex();
//发送MSG_SWITCH_STATE消息,并将index传递过去
CCNotificationCenter::sharedNotificationCenter()->postNotification(MSG_SWITCH_STATE, (CCObject*)index);
}
CCNotificationCenter(二)---NotificationCenterTest的更多相关文章
- 【小程序分享篇 二 】web在线踢人小程序,维持用户只能在一个台电脑持登录状态
最近离职了, 突然记起来还一个小功能没做, 想想也挺简单,留下代码和思路给同事做个参考. 换工作心里挺忐忑, 对未来也充满了憧憬与担忧.(虽然已是老人, 换了N次工作了,但每次心里都和忐忑). 写写代 ...
- 前端开发中SEO的十二条总结
一. 合理使用title, description, keywords二. 合理使用h1 - h6, h1标签的权重很高, 注意使用频率三. 列表代码使用ul, 重要文字使用strong标签四. 图片 ...
- 【疯狂造轮子-iOS】JSON转Model系列之二
[疯狂造轮子-iOS]JSON转Model系列之二 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇<[疯狂造轮子-iOS]JSON转Model系列之一> ...
- 【原】Android热更新开源项目Tinker源码解析系列之二:资源文件热更新
上一篇文章介绍了Dex文件的热更新流程,本文将会分析Tinker中对资源文件的热更新流程. 同Dex,资源文件的热更新同样包括三个部分:资源补丁生成,资源补丁合成及资源补丁加载. 本系列将从以下三个方 ...
- 谈谈一些有趣的CSS题目(十二)-- 你该知道的字体 font-family
开本系列,谈谈一些有趣的 CSS 题目,题目类型天马行空,想到什么说什么,不仅为了拓宽一下解决问题的思路,更涉及一些容易忽视的 CSS 细节. 解题不考虑兼容性,题目天马行空,想到什么说什么,如果解题 ...
- MIP改造常见问题二十问
在MIP推出后,我们收到了很多站长的疑问和顾虑.我们将所有疑问和顾虑归纳为以下二十个问题,希望对大家理解 MIP 有帮助. 1.MIP 化后对其他搜索引擎抓取收录以及 SEO 的影响如何? 答:在原页 ...
- 如何一步一步用DDD设计一个电商网站(二)—— 项目架构
阅读目录 前言 六边形架构 终于开始建项目了 DDD中的3个臭皮匠 CQRS(Command Query Responsibility Segregation) 结语 一.前言 上一篇我们讲了DDD的 ...
- ASP.NET Core 之 Identity 入门(二)
前言 在 上篇文章 中讲了关于 Identity 需要了解的单词以及相对应的几个知识点,并且知道了Identity处在整个登入流程中的位置,本篇主要是在 .NET 整个认证系统中比较重要的一个环节,就 ...
- MVVM模式和在WPF中的实现(二)数据绑定
MVVM模式解析和在WPF中的实现(二) 数据绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...
随机推荐
- UISlider设置按钮透明
UISlider *aslider = [[UISlider alloc]initWithFrame:kCR(, , , )]; [aslider setValue:0.5]; [aslider se ...
- exception ORA-00918: 未明确定义列
exception ORA-00918: 未明确定义列 CreateTime--2018年5月9日16:08:48 Author:Marydon 1.错误代码展示 SELECT G.* FROM ...
- 【Oracle】函数
函数一般用于计算和返回一个值,可以将经常需要使用的计算或功能写成一个函数. 语法 create [or replace] function func_name[(parameter1,[,parame ...
- div最小高度的2种写法
1.第一种写法: 原理:在IE6中,使用CSS定义div的高度的时候经常遇到这个问题,就是当div的最小高度小于一定的值以后,就会发现,无论你怎么设置最小高度,div的高度会固定在一个值不再发生变动, ...
- IDEA的maven项目中静态文件编译的路径问题(未测试)
转自:http://www.cnblogs.com/signheart/p/6625126.html IDEA的maven项目中,默认源代码目录下的xml等资源文件并不会在编译的时候一块打包进clas ...
- DirectoryEntry配置IIS7出现ADSI Error:未知错误(0x80005000) [转]
一.错误情况 环境:win7+iis7.0 DirectoryEntry配置IIS7出现如下错误 或者是 下面一段代码在IIS6.0下运转正常,但IIS7.0下运转会出错: System.Direct ...
- 工作总结 ModelState.AddModelError("ShiYiObject", "对象不能为空!"); 小知识
// // 摘要: // 获取包含模型状态和模型绑定验证状态的模型状态字典对象. // // 返回结果: // 模型状态字典. public ModelStateDictionary ModelSta ...
- Ubuntu Pycharm不能同时选中多行解决方法
转自http://blog.csdn.net/yaoqi_isee/article/details/77866309 问题描述 Pycharm和Sublime有一个很好用的特性就是可以同时选中多行进行 ...
- VS报:"dll标记为系统必备组件,必须对其进行强签名"错误
问题: VS生成程序时,报“要将程序集“XX.dll”标记为系统必备组件,必须对其进行强签名.”错误. 解决方法: 1)在报错的解决方案中找到一个可以发布的项目(引用该XX.dll的项目未必可以发布) ...
- PO_已交货PO进行退货(流程)
2014-06-04 Created By BaoXinjian