//类的定义

#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的更多相关文章

  1. 【小程序分享篇 二 】web在线踢人小程序,维持用户只能在一个台电脑持登录状态

    最近离职了, 突然记起来还一个小功能没做, 想想也挺简单,留下代码和思路给同事做个参考. 换工作心里挺忐忑, 对未来也充满了憧憬与担忧.(虽然已是老人, 换了N次工作了,但每次心里都和忐忑). 写写代 ...

  2. 前端开发中SEO的十二条总结

    一. 合理使用title, description, keywords二. 合理使用h1 - h6, h1标签的权重很高, 注意使用频率三. 列表代码使用ul, 重要文字使用strong标签四. 图片 ...

  3. 【疯狂造轮子-iOS】JSON转Model系列之二

    [疯狂造轮子-iOS]JSON转Model系列之二 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇<[疯狂造轮子-iOS]JSON转Model系列之一> ...

  4. 【原】Android热更新开源项目Tinker源码解析系列之二:资源文件热更新

    上一篇文章介绍了Dex文件的热更新流程,本文将会分析Tinker中对资源文件的热更新流程. 同Dex,资源文件的热更新同样包括三个部分:资源补丁生成,资源补丁合成及资源补丁加载. 本系列将从以下三个方 ...

  5. 谈谈一些有趣的CSS题目(十二)-- 你该知道的字体 font-family

    开本系列,谈谈一些有趣的 CSS 题目,题目类型天马行空,想到什么说什么,不仅为了拓宽一下解决问题的思路,更涉及一些容易忽视的 CSS 细节. 解题不考虑兼容性,题目天马行空,想到什么说什么,如果解题 ...

  6. MIP改造常见问题二十问

    在MIP推出后,我们收到了很多站长的疑问和顾虑.我们将所有疑问和顾虑归纳为以下二十个问题,希望对大家理解 MIP 有帮助. 1.MIP 化后对其他搜索引擎抓取收录以及 SEO 的影响如何? 答:在原页 ...

  7. 如何一步一步用DDD设计一个电商网站(二)—— 项目架构

    阅读目录 前言 六边形架构 终于开始建项目了 DDD中的3个臭皮匠 CQRS(Command Query Responsibility Segregation) 结语 一.前言 上一篇我们讲了DDD的 ...

  8. ASP.NET Core 之 Identity 入门(二)

    前言 在 上篇文章 中讲了关于 Identity 需要了解的单词以及相对应的几个知识点,并且知道了Identity处在整个登入流程中的位置,本篇主要是在 .NET 整个认证系统中比较重要的一个环节,就 ...

  9. MVVM模式和在WPF中的实现(二)数据绑定

    MVVM模式解析和在WPF中的实现(二) 数据绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...

随机推荐

  1. 在MyEclipse中设置jsp页面为默认utf-8编码(转)

    http://www.cnblogs.com/xdp-gacl/p/3496161.html 在MyEclispe中创建Jsp页面,Jsp页面的默认编码是“ISO-8859-1”,如下图所示: 在这种 ...

  2. Java从零开始学二十五(枚举定义和简单使用)

    一.枚举 枚举是指由一组固定的常量组成的类型,表示特定的数据集合,只是在这个数据集合定义时,所有可能的值都是已知的. 枚举常量的名称建议大写. 枚举常量就是枚举的静态字段,枚举常量之间使用逗号隔开. ...

  3. 4、Android Activity的生命周期 Activity的生命周期

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveXV4aWt1b18x/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...

  4. 算法笔记_127:蓝桥杯2017模拟赛-本科组习题解答(Java)

     目录 1 算年龄 2 猜算式 3 排列序数 4 字符串比较 5 还款计算 6 滑动解锁 7 风险度量   PS:以下代码部分仅供参考,若有不当之处,还请路过同学指出哦~ 1 算年龄 标题:算年龄 英 ...

  5. 解决Unable to load component class org.sonar.scanner.report.ActiveRulesPublisher/Unable to load component interface org.sonar.api.batch.rule.ActiveRules: NullPointerException

    解决办法 Delete the directory data/es in your SonarQube installation. Restart SonarQube.

  6. component和bean区别

    @Component and @Bean do two quite different things, and shouldn't be confused. @Component (and @Serv ...

  7. 【VMware】宿主机连接wifi,虚拟机中的Linux系统配置连接wifi

    环境描述 宿主机:Windows 10 64bit 虚拟机:Centos 第一步:虚拟机设置 选择连接方式为NAT 第二步:设置宿主机的wifi 控制面板>>网络和Internet> ...

  8. Knockout自定义绑定my97datepicker

    /* my97datepicker 时间格式'yyyy-MM-dd HH-mm-ss' */ ko.bindingHandlers.datetimes = { init: function (elem ...

  9. SSO之CAS + LDAP

    本来主要详细是介绍CAS和LDAP整合实现单点登录的步骤. 1. 依<SSO之安装CAS Server>所述安装好CAS Server.2. 安装ApacheDS.安装好ApacheDS后 ...

  10. GridView显示数据鼠标悬停变色

    一. 首先在前台GridView中加上onrowdatabound="GridView1_RowDataBound": <asp:GridView ID="Grid ...