플밍

CCNotification 을 이용한 메세지 송수신과 콜백함수 본문

프로그래밍/cocos2d-x

CCNotification 을 이용한 메세지 송수신과 콜백함수

너구리안주 2013. 10. 14. 12:01

cocos2d-x 에서는 윈도우의 sendMessage 나 postMessage 와 같이 메세지 송,수신 방법으로 콜백을 처리할 수 있다.

인터페이스를 만들어 구현한 다음 이벤트핸들러를 세팅하는 복잡한 구현을 간단하게 처리할 수 있다.

CCNotification 이 이런 방법을 제공한다.

#ads_1

- 메세지를 수신하는 측 -

메세지를 받을 곳에서 메세지수신자를 등록한다.

bool init(){

....

CCNotificationCenter::sharedNotificationCenter()->addObserver(this, callfuncO_selector(ActionLayer::receiveMessage), "hello", NULL);  //메세지박스에서 메세지를 꺼내 hello 라는 글자가 들어있으면 receiveMessage 를 실행시킨다

....

return true;

}

//콜백함수

void ActionLayer::receiveMessage(CCObject* pSender){

CCString* param = (CCString*)pSender;

CCLog("%s", param->getString());  //"hi~ every one" 이 출력됨

}



- 메세지를 발신하는 측 -

어떤 이벤트가 있을때 단순히 아래와 같이 송신하면 된다.

CCString* str = CCString::create("hi~ every one");

CCNotificationCenter::sharedNotificationCenter()->postNotification("hello", str);  //hello 라는 글자를 메세지 박스에 집어 넣는다

#ads_1










Comments