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

cocos2d-x 스케줄

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


●스케줄

- 특정시간마다 특정함수를 호출한다.

- 정지(pause), 재시작(resume), 스테이지로 들어가면 타이머 활성화 등을 사용하게 해준다.





●예제

- 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);

void doStart(CCObject* pSender);

void doPause(CCObject* pSender);

void doResume(CCObject* pSender);

void doChange(CCObject* pSender);

void doStop(CCObject* pSender);

void tick1(float f);

void tick2(float f);

int nNum;

bool bChange;


};


#endif // __HELLOWORLD_SCENE_H__





- HelloWorldScene.cpp


#include "HelloWorldScene.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* item1=CCMenuItemFont::create("start", this, menu_selector(HelloWorld::doStart));

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


CCMenuItemFont* item2=CCMenuItemFont::create("pause", this, menu_selector(HelloWorld::doPause));

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


CCMenuItemFont* item3=CCMenuItemFont::create("resume", this, menu_selector(HelloWorld::doResume));

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


CCMenuItemFont* item4=CCMenuItemFont::create("change", this, menu_selector(HelloWorld::doChange));

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


CCMenuItemFont* item5=CCMenuItemFont::create("stop", this, menu_selector(HelloWorld::doStop));

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


//메뉴생성

CCMenu* pMenu=CCMenu::create(item1, item2, item3, item4, item5, NULL);

pMenu->alignItemsVertically();

this->addChild(pMenu);


//변수초기화

nNum=0;

bChange=false;


    return true;

}


void HelloWorld::doStart(CCObject* pSender)

{

this->schedule(schedule_selector(HelloWorld::tick1), 1.0f);

this->schedule(schedule_selector(HelloWorld::tick2), 2.0f);

}


void HelloWorld::doPause(CCObject* pSender)

{

CCDirector::sharedDirector()->getScheduler()->pauseTarget(this);

}


void HelloWorld::doResume(CCObject* pSender)

{

CCDirector::sharedDirector()->getScheduler()->resumeTarget(this);

}


void HelloWorld::doChange(CCObject* pSender)

{

if(bChange){

bChange=false;

this->unschedule(schedule_selector(HelloWorld::tick2));

this->schedule(schedule_selector(HelloWorld::tick2), 2.0f);

}else{

bChange=true;

this->unschedule(schedule_selector(HelloWorld::tick2));

this->schedule(schedule_selector(HelloWorld::tick2),3.0f);

}

}


void HelloWorld::doStop(CCObject* pSender)

{

this->unschedule(schedule_selector(HelloWorld::tick1));

this->unschedule(schedule_selector(HelloWorld::tick2));

}


void HelloWorld::tick1(float f)  //스케줄로 호출하는 함수는 인자로 float를 가져야한다.

{

CCLog("tick1");

}



void HelloWorld::tick2(float f)

{

CCLog("tick2");

}










Scene내의 모든 액션과 스케줄을 정지, 재시작


- CCDirector::sharedDirector()->paruse()


- CCDirector::sharedDirector()->resume()









반응형

'프로그래밍 > cocos2d-x' 카테고리의 다른 글

cocos2d-x 패럴렉스노드(ParallaxNode)  (0) 2013.06.02
cocos2d-x CCUserDefault  (0) 2013.06.01
cocos2d-x 터치(Touch)  (0) 2013.06.01
cocos2d-x 심플오디오엔진(SimpleAudioEngine)  (0) 2013.05.31
cocos2d-x 애니메이션  (0) 2013.05.31

댓글