今天学习了Cocosd-x中场景和层的使用。
本次实例有两个场景HelloWord , Setting,在HelloWord的游戏设置中可以切换到Setting场景
首先我们新建一个Setting类,继承自cocos2d::Layer类
具体代码如下:
#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" class HelloWorld : public cocos2d::Layer { public: // there's no 'id' in cpp,so we recommend returning the class instance pointer static cocos2d::Scene* createScene(); // Here's a difference. Method 'init' in cocos2d-x returns bool,instead of returning 'id' in cocos2d-iphone virtual bool init(); // a selector callback //void menuCloseCallback(cocos2d::Ref* pSender); // void menuItemStartCallBack(cocos2d::Ref *pSender); void menuItemSettingCallBack(cocos2d::Ref *pSender); void menuItemHelpCallBack(cocos2d::Ref *pSender); // implement the "static create()" method manually CREATE_FUNC(HelloWorld); }; #endif // __HELLOWORLD_SCENE_H__
#include"SettingScene.h" USING_NS_CC; Scene *Setting::createScene(){ auto scene = Scene::create(); auto layer = Setting::create(); scene->addChild(layer); return scene; } bool Setting::init(){ if (!Layer::init()){ return FALSE; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); //背景创建 Sprite *bg = Sprite::create("setting-back.png"); bg->setPosition(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(Vec2(818,220))); //音乐设置 auto musicOnMenuItem = MenuItemImage::create("on.png","on.png"); auto musicOffMenuItem = MenuItemImage::create("off.png","off.png"); auto musicToggleMenuItem = MenuItemToggle::createWithCallback(CC_CALLBACK_1(Setting::menuMusicToggleCallBack,musicOnMenuItem,musicOffMenuItem,NULL); musicToggleMenuItem->setPosition(Director::getInstance()->convertToGL(Vec2(818,362))); //ok按钮 auto okMenuItem = MenuItemImage::create("ok-down.png","ok-up.png",CC_CALLBACK_1(Setting::menuOkCallBack,this)); okMenuItem->setPosition(Director::getInstance()->convertToGL(Vec2(600,510))); Menu* mn = Menu::create(soundToggleMenuItem,musicToggleMenuItem,okMenuItem,NULL); mn->setPosition(Vec2::ZERO); this->addChild(mn); return TRUE; } void Setting::menuSoundToggleCallBack(cocos2d::Ref* pSender){ } void Setting::menuMusicToggleCallBack(cocos2d::Ref* pSender){ } void Setting::menuOkCallBack(cocos2d::Ref* pSender){ Director::getInstance()->popScene(); }
以上就完成了对Setting类的封装,下面我们只要在HelloWorld场景中进行场景的切换就可以了
具体代码如下:
// on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); //添加背景图片 Sprite *bg = Sprite::create("background.png"); bg->setPosition(Vec2(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(Vec2(700,170))); //设置图片菜单 MenuItemImage *settingMenuItem = MenuItemImage::create( "setting-up.png","setting-down.png",CC_CALLBACK_1(HelloWorld::menuItemSettingCallBack,this)); settingMenuItem->setPosition(Director::getInstance()->convertToGL(Vec2(480,400))); //设置帮助菜单 MenuItemImage *helpMenuItem = MenuItemImage::create( "help-up.png","help-down.png",CC_CALLBACK_1(HelloWorld::menuItemHelpCallBack,this)); helpMenuItem->setPosition(Director::getInstance()->convertToGL(Vec2(860,480))); Menu *mn = Menu::create(StartMenuItem,settingMenuItem,helpMenuItem,NULL); mn->setPosition(Vec2::ZERO); this->addChild(mn); return true; } void HelloWorld::menuItemStartCallBack(cocos2d::Ref *pSender){ MenuItem *item = (MenuItem*)pSender; log("Touch Start %p ",item); } void HelloWorld::menuItemSettingCallBack(cocos2d::Ref *pSender){ auto sc = Setting::createScene(); Director::getInstance()->pushScene(sc); } void HelloWorld::menuItemHelpCallBack(cocos2d::Ref *pSender){ }
具体使用到的场景切换函数:
void pushScene(); //切换到下一个场景,将当前场景挂起到场景堆栈中,然后切换到下一个场景
void popScene(); //回到上一个场景
以及场景过度动画:
auto sc = Setting::createScene();
auto reScene = TransitionJumpZoom::create(2.0f,sc);
Director::getInstance()->pushScene(reScene);
关键代码就是第二句动画的设置。具体动画类型去查看过渡动画类TransitionScene类和它的自雷
原文链接:https://www.f2er.com/cocos2dx/343865.html