首页 > 代码库 > cocos2d-x实战 C++卷 学习笔记--第6章 场景与层
cocos2d-x实战 C++卷 学习笔记--第6章 场景与层
前言:
一个场景(Scene)是由多个层(Layer)组成,而且层的个数要至少是1,不能为0。
场景切换
场景切换相关函数
1)void runWithScene(Scene* scene)
该函数可以运行场景。只能在启动第一个场景时调用该函数。如果已经有一个场景运行,则不能调用该函数。
2)void replaceScene(Scene* scene)
切换到下一个场景。用一个新的场景替换当前场景,当前场景被终端释放。
3)void pushScene(Scene* scene)
切换到下一个场景。将当前场景挂起放入到场景堆栈中,然后再切换到下一个场景中。
4)void popScene(Scene* scene)
与 pushScene 配合使用,可以回到上一个场景。
5)void popToRootScene(Scene* scene)
与 pushScene 配合使用,可以回到根场景。
代码示例:
1 #ifndef __HELLOWORLD_SCENE_H__ 2 #define __HELLOWORLD_SCENE_H__ 3 4 #include "cocos2d.h" 5 #include "Setting.h" 6 7 class HelloWorld : public cocos2d::Layer 8 { 9 public:10 // there‘s no ‘id‘ in cpp, so we recommend returning the class instance pointer11 static cocos2d::Scene* createScene();12 13 // Here‘s a difference. Method ‘init‘ in cocos2d-x returns bool, instead of returning ‘id‘ in cocos2d-iphone14 virtual bool init(); 15 16 // a selector callback17 void menuCloseCallback(cocos2d::Ref* pSender);18 19 //// 20 void menuItemStartCallback(Ref* pSender);21 void menuItemSettingCallback(Ref* pSender);22 void menuItemHelpCallback(Ref* pSender);23 24 // implement the "static create()" method manually25 CREATE_FUNC(HelloWorld);26 };27 28 #endif // __HELLOWORLD_SCENE_H__
1 #include "HelloWorldScene.h" 2 3 USING_NS_CC; 4 5 Scene* HelloWorld::createScene() 6 { 7 // ‘scene‘ is an autorelease object 8 auto scene = Scene::create(); 9 10 // ‘layer‘ is an autorelease object 11 auto layer = HelloWorld::create(); 12 13 // add layer as a child to scene 14 scene->addChild(layer); 15 16 // return the scene 17 return scene; 18 } 19 20 // on "init" you need to initialize your instance 21 bool HelloWorld::init() 22 { 23 ////////////////////////////// 24 // 1. super init first 25 if ( !Layer::init() ) 26 { 27 return false; 28 } 29 30 Size visibleSize = Director::getInstance()->getVisibleSize(); 31 Point origin = Director::getInstance()->getVisibleOrigin(); 32 33 ///////////////////////////// 34 // 2. add a menu item with "X" image, which is clicked to quit the program 35 // you may modify it. 36 37 // add a "close" icon to exit the progress. it‘s an autorelease object 38 auto closeItem = MenuItemImage::create( 39 "CloseNormal.png", 40 "CloseSelected.png", 41 CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); 42 43 closeItem->setPosition(Point(origin.x + visibleSize.width - closeItem->getContentSize().width/2 , 44 origin.y + closeItem->getContentSize().height/2)); 45 46 // create menu, it‘s an autorelease object 47 auto menu = Menu::create(closeItem, NULL); 48 menu->setPosition(Point::ZERO); 49 this->addChild(menu, 1); 50 51 ///////////////////////////// 52 // 3. add your codes below... 53 54 // add a label shows "Hello World" 55 // create and initialize a label 56 57 auto label = LabelTTF::create("Hello World", "Arial", 24); 58 59 // position the label on the center of the screen 60 label->setPosition(Point(origin.x + visibleSize.width/2, 61 origin.y + visibleSize.height - label->getContentSize().height)); 62 63 // add the label as a child to this layer 64 this->addChild(label, 1); 65 66 // add "HelloWorld" splash screen" 67 auto sprite = Sprite::create("HelloWorld.png"); 68 69 // position the sprite on the center of the screen 70 sprite->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); 71 72 // add the sprite as a child to this layer 73 this->addChild(sprite, 0); 74 75 ////// "开始" 精灵菜单 76 Sprite* startSpriteNormal = Sprite::create("Demo1/start-up.png"); 77 Sprite* startSpriteSelected = Sprite::create("Demo1/start-down.png"); 78 MenuItemSprite* startMenuItem = MenuItemSprite::create(startSpriteNormal, 79 startSpriteSelected, 80 CC_CALLBACK_1(HelloWorld::menuItemStartCallback, this)); 81 startMenuItem->setPosition(Director::getInstance()->convertToGL(Point(700, 170))); 82 83 /// "设置" 图片菜单 84 MenuItemImage* settingMenuItem = MenuItemImage::create( 85 "Demo1/setting-up.png", 86 "Demo1/setting-down.png", 87 CC_CALLBACK_1(HelloWorld::menuItemSettingCallback, this)); 88 settingMenuItem->setPosition(Director::getInstance()->convertToGL(Point(480, 400))); 89 90 /// "帮助" 图片菜单 91 MenuItemImage* helpMenuItem = MenuItemImage::create( 92 "Demo1/help-up.png", 93 "Demo1/help-down.png", 94 CC_CALLBACK_1(HelloWorld::menuItemHelpCallback, this)); 95 helpMenuItem->setPosition(Director::getInstance()->convertToGL(Point(860, 480))); 96 97 Menu* mu = Menu::create(startMenuItem, settingMenuItem, helpMenuItem, NULL); 98 mu->setPosition(Point::ZERO); 99 this->addChild(mu);100 101 102 103 104 return true;105 }106 107 108 void HelloWorld::menuCloseCallback(Ref* pSender)109 {110 #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)111 MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");112 return;113 #endif114 115 Director::getInstance()->end();116 117 #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)118 exit(0);119 #endif120 }121 122 123 void HelloWorld::menuItemStartCallback(Ref* pSender)124 {125 MenuItem* item = (MenuItem*)pSender;126 log("Touch Start %p", item);127 128 auto sc = Setting::createScene();129 // Director::getInstance()->replaceScene(sc);130 Director::getInstance()->pushScene(sc);131 }132 133 void HelloWorld::menuItemSettingCallback(Ref* pSender)134 {135 MenuItem* item = (MenuItem*)pSender;136 log("Touch Setting %p", item);137 138 }139 140 void HelloWorld::menuItemHelpCallback(Ref* pSender)141 {142 MenuItem* item = (MenuItem*)pSender;143 log("Touch Help %p", item);144 145 }
1 #ifndef __SETTING_SCENE_H__ 2 #define __SETTING_SCENE_H__ 3 4 #include "cocos2d.h" 5 6 class Setting : public cocos2d::Layer 7 { 8 public: 9 static cocos2d::Scene* createScene();10 virtual bool init();11 12 void menuSoundToggleCallback(cocos2d::Ref* pSender);13 void menuMusicToggleCallback(cocos2d::Ref* pSender);14 void menuOkCallback(cocos2d::Ref* pSender);15 16 CREATE_FUNC(Setting);17 };18 19 #endif // __SETTING_SCENE_H__
1 #include "Setting.h" 2 3 // using namespace cocos2d; 4 USING_NS_CC; 5 6 Scene* Setting::createScene() 7 { 8 // ‘scene‘ is an autorelease object 9 auto scene = Scene::create();10 // ‘layer‘ is an autorelease object11 auto layer = Setting::create();12 13 scene->addChild(layer);14 return scene;15 }16 17 bool Setting::init()18 {19 if (!Layer::init())20 {21 return false;22 }23 Size visibleSize = Director::getInstance()->getVisibleSize();24 Point origin = Director::getInstance()->getVisibleOrigin();25 26 Sprite*bg = Sprite::create("Demo1/setting-back.png");27 bg->setPosition(Point(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2));28 this->addChild(bg);29 30 // 音效31 auto soundOnMenuItem = MenuItemImage::create(32 "Demo1/on.png",33 "Demo1/on.png");34 auto soundOffmenuItem = MenuItemImage::create(35 "Demo1/off.png",36 "Demo1/off.png");37 auto soundToggleMenuItem = MenuItemToggle::createWithCallback(38 CC_CALLBACK_1(Setting::menuSoundToggleCallback, this),39 soundOnMenuItem,40 soundOffmenuItem,41 NULL);42 soundToggleMenuItem->setPosition(Director::getInstance()->convertToGL(Point(818, 220)));43 44 // 音乐45 auto musicOnMenuItem = MenuItemImage::create(46 "Demo1/on.png",47 "Demo1/on.png");48 auto musicOffMenuItem = MenuItemImage::create(49 "Demo1/off.png",50 "Demo1/off.png");51 auto musicToggleItem = MenuItemToggle::createWithCallback(52 CC_CALLBACK_1(Setting::menuMusicToggleCallback, this),53 musicOnMenuItem,54 musicOffMenuItem,55 NULL);56 musicToggleItem->setPosition(Director::getInstance()->convertToGL(Point(818, 362)));57 58 // OK 按钮59 auto okMenuItem = MenuItemImage::create(60 "Demo1/ok-down.png",61 "Demo1/ok-up.png",62 CC_CALLBACK_1(Setting::menuOkCallback, this));63 okMenuItem->setPosition(Director::getInstance()->convertToGL(Point(600, 510)));64 65 Menu* mn = Menu::create(soundToggleMenuItem,66 musicToggleItem,67 okMenuItem,68 NULL);69 mn->setPosition(Point::ZERO);70 this->addChild(mn);71 72 return true;73 }74 75 76 void Setting::menuSoundToggleCallback(cocos2d::Ref* pSender)77 {78 MenuItem* item = (MenuItem*)pSender;79 log("Touch Setting::Music %p", item);80 }81 82 83 void Setting::menuMusicToggleCallback(cocos2d::Ref* pSender)84 {85 MenuItem* item = (MenuItem*)pSender;86 log("Touch Setting::Music %p", item);87 }88 89 90 void Setting::menuOkCallback(cocos2d::Ref* pSender)91 {92 MenuItem* item = (MenuItem*)pSender;93 log("Touch Setting::Ok %p", item);94 Director::getInstance()->popScene();95 }
关键代码解释:
1)使用 replaceScene
auto sc = Setting::createScene();
Director->getInstance()->replaceScene(sc);
用 Setting 场景,替换当前的场景。当前场景被是否。
2)使用 pushScene
auto sc = Setting::createScene();
Director->getInstance()->pushScene(sc);
将当前场景挂起放入到场景堆栈中,然后切换到 Setting 场景
3)使用 popScene
Director->getInstance()->popScene();
将当前场景释放,从 场景堆栈的栈顶 调入 一个 场景到程序中。(如果场景堆栈中没有场景,那么程序退出)
cocos2d-x实战 C++卷 学习笔记--第6章 场景与层