CCNotificationCenter

1.公共层


PublicObject.h头文件中:

#define MY_NOTIFICATION "MY_NOTIFICATION"

static void useCoinBuyItem(Item_Type type);


PublicObject.ccp文件中:

void PublicObject::useCoinBuyItem(Item_Type type)
{

switch (type)
{
case MAGIC:

CCNotificationCenter::sharedNotificationCenter()->postNotification(MY_NOTIFICATION, (CCObject*)MAGIC);
break;

        }


}




2.具体某个CCScene 或者 CCLayer

#include "StoreScene.h"

#include "PublicObject.h"


bool StoreScene::init() 方法中

//添加侦听事件***********
CCNotificationCenter::sharedNotificationCenter()->addObserver(this, callfuncO_selector(StoreScene::myNotificationDisplayNum), MY_NOTIFICATION, NULL);


析构方法中

StoreScene::~StoreScene()
{
//移除监听事件
CCNotificationCenter::sharedNotificationCenter()->removeObserver(this, MY_NOTIFICATION);

// int removeAllObservers = CCNotificationCenter::sharedNotificationCenter()->removeAllObservers(this);
}


void StoreScene::myNotificationDisplayNum(Item_Type type)
{

.........................................

}





你可能感兴趣的:(ios,Mac)