cocos2dx 화면 전환하기


코코스2dx를 이용해서 앱을 개발할때 사용하는 방법입니다.


저는 TranstionServices 라고 명명한 클래스를 만들어서 사용합니다.





헤더파일

class TransitionServices

{

public:

TransitionServices();

~TransitionServices();


// attributes

public:

static void ReplaceToIntroScene();

static void ReplaceToMainScene();

static void ReplaceToGameScene();

};


소스파일


//---------------------------------------------------------------------EWST API

#include "TransitionServices.h"

#include "MainScene.h"

#include "IntroScene.h"

#include "GameScene.h"


#define TRANSITION_TIME 0.5f


//-----------------------------------------------------------------------------

//Constructor : TransitionServices

//-----------------------------------------------------------------------------

TransitionServices::TransitionServices()

{

}


//-----------------------------------------------------------------------------

//Destructor : ~TransitionServices

//-----------------------------------------------------------------------------

TransitionServices::~TransitionServices()

{

}


//-----------------------------------------------------------------------------

// ReplaceToMainScene

//-----------------------------------------------------------------------------

void TransitionServices::ReplaceToIntroScene()

{

auto scene = IntroScene::createScene();

Scene* pTransition = TransitionFade::create(TRANSITION_TIME, scene);

if (pTransition)

Director::getInstance()->replaceScene(pTransition);

}



//-----------------------------------------------------------------------------

// ReplaceToMainScene

//-----------------------------------------------------------------------------

void TransitionServices::ReplaceToMainScene()

{

auto scene = MainScene::createScene();

Scene* pTransition = TransitionFade::create(TRANSITION_TIME, scene);

if (pTransition)

Director::getInstance()->replaceScene(pTransition);

}


//-----------------------------------------------------------------------------

// ReplaceToGameScene

//-----------------------------------------------------------------------------

void TransitionServices::ReplaceToGameScene()

{

auto scene = GameScene::createScene();

Scene* pTransition = TransitionFade::create(TRANSITION_TIME, scene);

if (pTransition)

Director::getInstance()->replaceScene(pTransition);

}


이렇게 만들어 놓으면 다음 앱 개발할때 저 TransitionServices 클래스만 복사해 와서 그대로 사용하면 되니까 여러모로 편리하더라구요


사용방법은?


#include "TransitionServices.h"


void MainScene::login()

{

TransitionServices::ReplaceToMainScene();

}


이렇게 하면 된답니다.

간단하죠?


Scene 전환시 사용되는 효과는 여러가지가 있으니 사용처에 맞게 구현하면 될듯합니다.