●장면 추가,삭제,교체
- HelloWorldScene.h
#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" #include "Box2D/Box2D.h" #include "SimpleAudioEngine.h" class HelloWorld : public cocos2d::CCLayerColor { public: virtual bool init(); static cocos2d::CCScene* scene();
CREATE_FUNC(HelloWorld);
void doPushScene(CCObject* pSender); void doReplaceScene(CCObject* pSender);
}; #endif // __HELLOWORLD_SCENE_H__ |
- HelloWorldScene.cpp
#include "HelloWorldScene.h" #include "TestScene1.h" #include "TestScene2.h" using namespace cocos2d; CCScene* HelloWorld::scene() { CCScene *scene=CCScene::create(); HelloWorld *layer=HelloWorld::create();
scene->addChild(layer); return scene;
} // on "init" you need to initialize your instance bool HelloWorld::init() { if(!CCLayerColor::initWithColor(ccc4(255,255,255,255))) { return false; } /////////////////////// //메뉴 아이템 생성 및 초기화 CCMenuItemFont* pMenuItem1=CCMenuItemFont::create( "pushScene", this, menu_selector(HelloWorld::doPushScene)); pMenuItem1->setColor(ccc3(0,0,0));
CCMenuItemFont* pMenuItem2=CCMenuItemFont::create( "replaceScene", this, menu_selector(HelloWorld::doReplaceScene)); pMenuItem2->setColor(ccc3(0,0,0));
//메뉴 생성 CCMenu* pMenu=CCMenu::create(pMenuItem1,pMenuItem2, NULL);
//메뉴 위치 지정 pMenu->setPosition(ccp(240, 50)); //메뉴 정렬 pMenu->alignItemsVertically(); //레이어에 메뉴 객체 추가 this->addChild(pMenu);
return true; } //doPushScene 메서드 정의 void HelloWorld::doPushScene(CCObject* pSender) { CCScene* pScene=TestScene1::scene(); CCDirector::sharedDirector()->pushScene(pScene); //기존장면 두고 새장면 올리기 } //doReplaceScene void HelloWorld::doReplaceScene(CCObject* pSender) { CCScene* pScene=TestScene2::scene(); CCDirector::sharedDirector()->replaceScene(pScene);//기존장면 없애고 새장면 교체 } |
- TestScene1.h
#ifndef __TestScene1_H__ #define __TestScene1_H__ #include "cocos2d.h" #include "Box2D/Box2D.h" #include "SimpleAudioEngine.h" class TestScene1 : public cocos2d::CCLayerColor { public: virtual bool init(); static cocos2d::CCScene* scene();
CREATE_FUNC(TestScene1); void doClose(CCObject* pSender); }; #endif // __HELLOWORLD_SCENE_H__ |
- TestScene1.cpp
#include "TestScene1.h" using namespace cocos2d; CCScene* TestScene1::scene() { CCScene *scene=CCScene::create(); TestScene1 *layer=TestScene1::create();
scene->addChild(layer); return scene;
} // on "init" you need to initialize your instance bool TestScene1::init() { if(!CCLayerColor::initWithColor(ccc4(0,255,0,255))) { return false; } ////////////////////// //메뉴 아이템 생성 및 초기화 CCMenuItemFont* Item1=CCMenuItemFont::create( "Close", this, menu_selector(TestScene1::doClose)); Item1->setColor(ccc3(0,0,0)); //메뉴 생성 CCMenu* pMenu=CCMenu::create(Item1, NULL); //메뉴 위치 지정 pMenu->setPosition(ccp(240, 50)); //레이어에 메뉴 객체 추가 this->addChild(pMenu);
return true; } void TestScene1::doClose(CCObject* pSender) { CCDirector::sharedDirector()->popScene(); //pushScene으로 연 장면 닫기 } |
- TestScene2.h
#ifndef __TestScene2_H__ #define __TestScene2_H__ #include "cocos2d.h" #include "Box2D/Box2D.h" #include "SimpleAudioEngine.h" class TestScene2 : public cocos2d::CCLayerColor { public: virtual bool init(); static cocos2d::CCScene* scene();
CREATE_FUNC(TestScene2);
void doClose(CCObject* pSender);
}; #endif // __HELLOWORLD_SCENE_H__ |
- TestScene2.cpp
#include "TestScene2.h" #include "HelloWorldScene.h" using namespace cocos2d; CCScene* TestScene2::scene() { CCScene *scene=CCScene::create(); TestScene2 *layer=TestScene2::create();
scene->addChild(layer); return scene;
} // on "init" you need to initialize your instance bool TestScene2::init() { if(!CCLayerColor::initWithColor(ccc4(255,255,0,255))) { return false; } ///////////////////////
//메뉴 아이템 생성 및 초기화 CCMenuItemFont* Item1=CCMenuItemFont::create( "Close Scene", this, menu_selector(TestScene2::doClose)); Item1->setColor(ccc3(0,0,0));
//메뉴 생성 CCMenu* pMenu=CCMenu::create(Item1, NULL);
//메뉴 위치 지정 pMenu->setPosition(ccp(240, 50)); //레이어에 메뉴 객체 추가 this->addChild(pMenu);
return true; } void TestScene2::doClose(CCObject* pSender) { CCScene* pScene=HelloWorld::scene(); //장면을 만들고 CCDirector::sharedDirector()->replaceScene(pScene); //교체한다 }
|
'프로그래밍 > cocos2d-x' 카테고리의 다른 글
cocos2d-x 심플오디오엔진(SimpleAudioEngine) (0) | 2013.05.31 |
---|---|
cocos2d-x 애니메이션 (0) | 2013.05.31 |
cocos2d-x 액션3 (0) | 2013.05.29 |
cocos2d-x 액션2 (0) | 2013.05.28 |
cocos2d-x 액션1 (0) | 2013.05.28 |
댓글