場景切換是經過導演類Director實現的,其中的相關函數以下:php
runWithScene(Scene* scene)。該函數能夠運行場景。只能在啓動第一個場景時候調用該函數。若是已經有一個場景運行狀況下則不能調用該函數。html
replaceScene(Scene* scene)。切換到下一個場景。用一個新的場景替換當前場景,當前場景被終端釋放。函數
pushScene(Scene* scene)。切換到下一個場景。將當前場景掛起放入到場景堆棧中,而後再切換到下一個場景中。網站
void popScene()。與pushScene配合使用,能夠回到上一個場景。this
void popToRootScene()。與pushScene配合使用,能夠回到根場景。spa
咱們須要注意replaceScene和pushScene使用的區別。replaceScene會釋放和銷燬場景,若是須要保持原來場景的狀態,replaceScene函數不適合。pushScene並不會釋放和銷燬場景,原來場景的狀態能夠保持,可是場景不能放太多的場景對象。.net
使用replaceScene代碼以下:code
[html] view plaincopyorm
auto sc = Setting::createScene(); htm
Director::getInstance()->replaceScene(sc);
其中的Setting是下一個要切換的場景。使用pushScene代碼以下:
[html] view plaincopy
auto sc = Setting::createScene();
Director::getInstance()->pushScene(sc);
從Setting場景回到上一個場景使用代碼以下:
[html] view plaincopy
Director::getInstance()->popScene();
下面咱們經過一個實例場景切換相關函數,以下圖所示有兩個場景:HelloWorld和Setting(設置)。在HelloWorld場景點擊「遊戲設置」菜單能夠切換到Setting場景,在Setting場景中點擊「OK」菜單能夠返回到HelloWorld場景。
咱們首先須要在工程中添加一個Setting場景(SettingScene.cpp和SettingScene.h),以下圖所示,右鍵點擊HelloWorld工程中Classes文件夾,在彈出菜單中選擇「添加」→「新建項」:
彈出對話框以下圖所示,在名稱中輸入「SettingScene.cpp」,並選擇保存文件位置,而後點擊「添加」按鈕建立SettingScene.cpp。使用一樣的方法建立SettingScene.h。
下面咱們看看代碼部分,HelloWorldScene.cpp中的重要代碼以下:
[html] view plaincopy
bool HelloWorld::init()
{
if ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Point origin = Director::getInstance()->getVisibleOrigin();
Sprite *bg = Sprite::create("background.png");
bg->setPosition(Point(origin.x + visibleSize.width/2,
origin.y +visibleSize.height /2));
this->addChild(bg);
//開始精靈
Sprite *startSpriteNormal = Sprite::create("start-up.png");
Sprite *startSpriteSelected =Sprite::create("start-down.png");
MenuItemSprite*startMenuItem = MenuItemSprite::create(startSpriteNormal,
startSpriteSelected,
CC_CALLBACK_1(HelloWorld::menuItemStartCallback,this));
startMenuItem->setPosition(Director::getInstance()->convertToGL(Point(700,170)));
//設置圖片菜單
MenuItemImage *settingMenuItem = MenuItemImage::create(
"setting-up.png",
"setting-down.png",
CC_CALLBACK_1(HelloWorld::menuItemSettingCallback,this));
settingMenuItem->setPosition(Director::getInstance()->convertToGL(Point(480,400)));
//幫助圖片菜單
MenuItemImage *helpMenuItem = MenuItemImage::create(
"help-up.png",
"help-down.png",
CC_CALLBACK_1(HelloWorld::menuItemHelpCallback,this));
helpMenuItem->setPosition(Director::getInstance()->convertToGL(Point(860,480)));
Menu*mu = Menu::create(startMenuItem, settingMenuItem, helpMenuItem, NULL);
mu->setPosition(Point::ZERO);
this->addChild(mu);
return true;
}
voidHelloWorld::menuItemSettingCallback(Ref* pSender) ①
{
autosc = Setting::createScene(); ②
//Director::getInstance()->replaceScene(sc); ③
Director::getInstance()->pushScene(sc); ④
}
void HelloWorld::menuItemHelpCallback(Ref*pSender)
{
MenuItem* item = (MenuItem*)pSender;
log("TouchHelp %p", item);
}
void HelloWorld::menuItemStartCallback(Ref*pSender)
{
MenuItem* item = (MenuItem*)pSender;
log("TouchStart %p", item);
}
上述代碼中的第①行定義的函數menuItemSettingCallback,是在用戶點擊「遊戲設置」菜單時候回調。第②行代碼是建立設置建立對象。第③行是使用replaceScene函數進行場景切換。第④行代碼是pushScene函數進行場景切換。
HelloWorldScene.cpp中的重要代碼以下:
[html] view plaincopy
bool Setting::init()
{
if ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Point origin = Director::getInstance()->getVisibleOrigin();
Sprite *bg = Sprite::create("setting-back.png");
bg->setPosition(Point(origin.x + visibleSize.width/2,
origin.y +visibleSize.height /2));
this->addChild(bg);
//音效
auto soundOnMenuItem = MenuItemImage::create(
"on.png",
"on.png");
auto soundOffMenuItem = MenuItemImage::create(
"off.png",
"off.png");
auto soundToggleMenuItem = MenuItemToggle::createWithCallback(
CC_CALLBACK_1(Setting::menuSoundToggleCallback,this),
soundOnMenuItem,
soundOffMenuItem,
NULL);
soundToggleMenuItem->setPosition(Director::getInstance()->convertToGL(Point(818,220)));
//音樂
auto musicOnMenuItem =MenuItemImage::create(
"on.png",
"on.png");
automusicOffMenuItem =MenuItemImage::create(
"off.png",
"off.png");
auto musicToggleMenuItem = MenuItemToggle::createWithCallback(
CC_CALLBACK_1(Setting::menuMusicToggleCallback,this),
musicOnMenuItem,
musicOffMenuItem,
NULL);
musicToggleMenuItem->setPosition(Director::getInstance()->convertToGL(Point(818,362)));
//Ok按鈕
autookMenuItem = MenuItemImage::create(
"ok-down.png",
"ok-up.png",
CC_CALLBACK_1(Setting::menuOkCallback, this));
okMenuItem->setPosition(Director::getInstance()->convertToGL(Point(600,510)));
Menu* mn = Menu::create(soundToggleMenuItem,
musicToggleMenuItem,okMenuItem,NULL);
mn->setPosition(Point::ZERO);
this->addChild(mn);
return true;
}
void Setting::menuOkCallback(Ref*pSender) ①
{
Director::getInstance()->popScene(); ②
}
void Setting::menuSoundToggleCallback(Ref*pSender)
{}
void Setting::menuMusicToggleCallback(Ref*pSender)
{}
上述代碼中的第①行定義的函數menuOkCallback,是在用戶在設置場景點擊「OK」菜單時候回調。第②行代碼是使用popScene函數返回HelloWorld場景。
更多內容請關注Cocos2d-x系列圖書《Cocos2d-x實戰(卷Ⅰ):C++開發》
本書交流討論網站:http://www.cocoagame.net
歡迎加入cocos2d-x技術討論羣:25776038六、327403678