본문 바로가기
프로그래밍/cocos2d-x

cocos2d-x 게임 중 일시정지하고 팝업창 띄우기(pause, resume)

by -현's- 2013. 6. 9.
반응형




●게임 중 화면을 정지 버튼을 누르면 일시정지하고 팝업창을 띄운다. 팝업창에서 버튼은 누르면 다시 게임 화면으로 돌아간다. 팝업창을 띄우고 게임상의 액션,스케줄을 멈추고 재시작하기 위해 " CCDirector::sharedDirector()->pause(); " 을 사용하고 정지버튼터치를 비활성하기 위해 " CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(pMenu, kCCMenuHandlerPriority,true); "를 사용한다. 팝업창이 열렸을때, 닫혔을때 구분해서 함수를 실행하기 위해 노티피케이션(Notification)을 이용한다. 노티피케이션은 클래스사이에서 메시지를 주고 받고 하는 싱클톤 객체이다.





●예제

- HelloWorldScene.h


#ifndef __HELLOWORLD_SCENE_H__

#define __HELLOWORLD_SCENE_H__


#include "cocos2d.h"


class HelloWorld : public cocos2d::CCLayerColor

{

public:

    virtual bool init();


    static cocos2d::CCScene* scene();

    

    CREATE_FUNC(HelloWorld);

cocos2d::CCMenu* pMenu ;


void HelloWorld::doPop(CCObject* pSender);

void HelloWorld::doNotification(CCObject *obj);


};


#endif // __HELLOWORLD_SCENE_H__

 




- HelloWorldScene.cpp


#include "HelloWorldScene.h"

#include "PopLayer.h"


using namespace cocos2d;


CCScene* HelloWorld::scene()

{

    CCScene *scene = CCScene::create();

    

    HelloWorld *layer = HelloWorld::create();


    scene->addChild(layer);


    return scene;

}


bool HelloWorld::init()

{

    if ( !CCLayerColor::initWithColor(ccc4(255, 255, 255, 255)) )

    {

        return false;

    }

////////////////////////


//메뉴 추가

CCMenuItemFont* itemPop=CCMenuItemFont::create("popUp", this, 

menu_selector(HelloWorld::doPop));

itemPop->setColor(ccc3(0,0,0));

//CCMenu* pMenu=CCMenu::create(itemPop, NULL);

pMenu=CCMenu::create(itemPop, NULL);

pMenu->setPosition(ccp(240, 50));

this->addChild(pMenu);

//스프라이트 추가

CCSprite* man=CCSprite::create("images/grossini.png");

man->setPosition( ccp(20,250));

this->addChild(man);


//액션 추가

CCActionInterval* myAction=CCMoveBy::create(15, ccp(400, 0));

man->runAction(myAction);



//노티피케이션 추카

CCNotificationCenter::sharedNotificationCenter()->addObserver(this,

callfuncO_selector(HelloWorld::doNotification),

"notification", NULL);

          //"notification"이라는 메시지가 오면 해당 함수를 실행한다.


    return true;

}


void HelloWorld::doPop(CCObject* pSender){

CCScene* pScene=PopLayer::scene();

this->addChild(pScene,2000,2000);



}

//노티피케이션 함수

void HelloWorld::doNotification(CCObject *obj){

      //노티피케이션 받기

CCString *pParam=(CCString*)obj;

CCLog("notification %s", pParam->getCString());

if(pParam->intValue()==1){

CCLog("noti 11");

CCDirector::sharedDirector()->resume();   //화면 재시작


CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(pMenu, kCCMenuHandlerPriority,true);

                                     //메뉴 버튼 활성

}

else{

CCArray* childs = this->getChildren();

CCLog("noti 00");

CCDirector::sharedDirector()->pause();   //화면 정지


CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(pMenu);

                                      //메뉴버튼 비활성

}


}

 





- PopLayer.h


#ifndef __PopLayer__

#define __PopLayer__


#include "cocos2d.h"


class PopLayer : public cocos2d::CCLayerColor

{

public:

    virtual bool init();


    static cocos2d::CCScene* scene();

    

    CREATE_FUNC(PopLayer);

cocos2d::CCSize winSize;

CCLayerColor* backLayer;

CCLayerColor* popUpLayer;

void PopLayer::doClose(CCObject* pSender);

};

#endif // _PopLayer__





- PopLayer.cpp


#include "PopLayer.h"

#include "HelloWorldScene.h"


using namespace cocos2d;


CCScene* PopLayer::scene()

{

    CCScene *scene = CCScene::create();

    

    PopLayer *layer = PopLayer::create();


    scene->addChild(layer);


    return scene;

}


bool PopLayer::init()

{

    if ( !CCLayerColor::initWithColor(ccc4(0, 0, 0, 0)) )  //투명하게

    {

        return false;

    }

////////////////////////


CCString* popParam=CCString::create("0");

CCNotificationCenter::sharedNotificationCenter()->postNotification("notification", popParam);         //노티피케이션 보내기


winSize=CCDirector::sharedDirector()->getWinSize();

//메뉴추가

CCMenuItemFont* pMenuItem=CCMenuItemFont::create("close", this, 

menu_selector(PopLayer::doClose) );

pMenuItem->setColor(ccc3(0,0,0));

CCMenu* pMenu2=CCMenu::create(pMenuItem, NULL);

pMenu2->setPosition(ccp(240, 100));

this->addChild(pMenu2,10);


//backLayer추가

backLayer=CCLayerColor::create(ccc4(0,0,0,50), winSize.width, winSize.height);

backLayer->setAnchorPoint(ccp(0,0));

backLayer->setPosition(ccp(0,0));

this->addChild(backLayer);


//popUpLayer추가

popUpLayer=CCLayerColor::create(ccc4(255,0,0,255), 250,150);

popUpLayer->setAnchorPoint(ccp(0,0));

popUpLayer->setPosition(ccp((winSize.width-popUpLayer->getContentSize().width)/2, 

(winSize.height-popUpLayer->getContentSize().height)/2,   )  );

this->addChild(popUpLayer);


    return true;

}



void PopLayer::doClose(CCObject* pSender)

{

CCString* popParam=CCString::create("1");

CCNotificationCenter::sharedNotificationCenter()->postNotification("notification", popParam);         //노티피케이션 보내기


//팝업창 제거

this->removeFromParentAndCleanup(true);


}












반응형

댓글