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 - 50, VisibleRect::rightBottom().y + 25));
    CCMenu* pBackMenu = CCMenu::create(pBackItem, NULL);
    pBackMenu->setPosition( CCPointZero );
    addChild(pBackMenu);
//创建总体开关按钮
    CCLabelTTF *label1 = CCLabelTTF::create("switch off", "Marker Felt", 26);
    CCLabelTTF *label2 = CCLabelTTF::create("switch on", "Marker Felt", 26);
    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(1);//默认选中的是第二个按钮选项
    CCMenu *menu = CCMenu::create(item, NULL);
    menu->setPosition(ccp(s.width/2+100, s.height/2));
    addChild(menu);

    CCMenu *menuConnect = CCMenu::create();
    menuConnect->setPosition(CCPointZero);
    addChild(menuConnect);
//创建三个按钮
    for (int i = 1; i <= 3; i++)
    {
//创建精灵
        Light* light = Light::lightWithFile("Images/Pea.png");
        light->setTag(kTagLight+i);
        light->setPosition(ccp(100, s.height/4*i));
        addChild(light);
//创建按钮点击的切换选项
        CCLabelTTF *label1 = CCLabelTTF::create("not connected", "Marker Felt", 26);
        CCLabelTTF *label2 = CCLabelTTF::create("connected", "Marker Felt", 26);
        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+50));
//加到菜单
        menuConnect->addChild(item, 0);
        if (i == 2)
        {
//最后一个菜单项默认是显示第二种
            item->setSelectedIndex(1);
        }
        bool bConnected = item->getSelectedIndex() == 1 ? 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(255);
    }
    else
    {
        this->setOpacity(50);
    }
}
//返回主菜单
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);
}
原文地址:https://www.cnblogs.com/newlist/p/3243159.html