cocos2d 消息映射

alayer



#include "ALayer.hpp"


ALayer::ALayer(void)
{
}


ALayer::~ALayer(void)
{
}

bool ALayer::init()
{
    bool bRet=false;
    do
    {
        CC_BREAK_IF(!CCLayer::init());
        
        bRet=true;
    } while (0);
    return bRet;
}



void ALayer::Post()
{
    CCLOG("pos");
    CCString* str=CCString::create("Hello BLayer!");
    CCNotificationCenter::sharedNotificationCenter()->postNotification("BMessage",str);
}


blayer



#include "BLayer.hpp"


BLayer::BLayer(void)
{
}


BLayer::~BLayer(void)
{
    CCNotificationCenter::sharedNotificationCenter()->purgeNotificationCenter();
}

bool BLayer::init()
{
    bool bRet=false;
    do
    {
        CC_BREAK_IF(!CCLayer::init());
        
        CCNotificationCenter::sharedNotificationCenter()->addObserver(this,callfuncO_selector(BLayer::getMessage),"BMessage",NULL);
        
        bRet=true;
    } while (0);
    return bRet;
}

void BLayer::getMessage(CCObject* obj)
{
    CCString* str=static_cast<CCString*>(obj);
    CCLog(str->getCString());
}

testlayer

    _aLayer=ALayer::create();
    this->addChild(_aLayer);
    
    _bLayer=BLayer::create();
    this->addChild(_bLayer);
    
    _aLayer->Post();
原文地址:https://www.cnblogs.com/yufenghou/p/5017633.html