转载请说明出处:http://www.jb51.cc/article/p-sirftvig-bas.html
github地址:https://github.com/teffy/cocos2dx
这几天接着看了一下场景切换的效果TransitionScene,而cocos也在系统中提供了很多种不同的切换效果,那么我们先看一下TransitionScene的类继承关系图:
每一个类在cocos2d的官网文档中有说明
而且在官方demo的cpp-test中也有demo,我也跟着写了一遍
首先是要理解一下一些宏定义
#define STRINGIFY(x) #x #define TRANS(__classname__){\//根据传入的类名构造一个结构体,结构体内有一个function和一个name,类似于java中XXX.class经过反射可以拿到很多东西 [](float t,Scene* s){ return __classname__::create(t,s);},\ STRINGIFY(__classname__),\ } struct transitions{//结构体 std::function<TransitionScene*(float t,Scene* s)> function; const char* name; }transitions[] = {//结构体数组,用来保存定义的类的create方法和name TRANS(MTransitionCrossFade),TRANS(MTransitionFade),TRANS(MTransitionFadeTR),TRANS(MTransitionFadeBL),TRANS(MTransitionFadeDown),TRANS(MTransitionFadeUp),TRANS(MTransitionJumpZoom),TRANS(MTransitionFlipAngular_DOWN),TRANS(MTransitionFlipAngular_LEFT),TRANS(MTransitionFlipAngular_RIGHT),TRANS(MTransitionFlipAngular_UP),TRANS(MTransitionFlipX_DOWN),TRANS(MTransitionFlipX_LEFT),TRANS(MTransitionFlipX_RIGHT),TRANS(MTransitionFlipX_UP),TRANS(MTransitionFlipY_DOWN),TRANS(MTransitionFlipY_LEFT),TRANS(MTransitionFlipY_RIGHT),TRANS(MTransitionFlipY_UP),TRANS(MTransitionZoomFlipAngular),TRANS(MTransitionZoomFlipX_DOWN),TRANS(MTransitionZoomFlipX_LEFT),TRANS(MTransitionZoomFlipX_RIGHT),TRANS(MTransitionZoomFlipX_UP),TRANS(MTransitionZoomFlipY_DOWN),TRANS(MTransitionZoomFlipY_LEFT),TRANS(MTransitionZoomFlipY_RIGHT),TRANS(MTransitionZoomFlipY_UP),TRANS(MTransitionShrinkGrow),TRANS(MTransitionSlideInL),TRANS(MTransitionSlideInB),TRANS(MTransitionSlideInR),TRANS(MTransitionSplitCols),TRANS(MTransitionSplitRows),TRANS(MTransitionTurnOffTiles),}; #define MAX_TRANSITION_COUNT (sizeof(transitions)/sizeof(transitions[0]))
TransitionScene* createTranstion(int _index,float t,Scene* s){ //Director::getInstance()->setDepthTest(false); return transitions[_index].function(t,s); }然后就是每一个切换效果的类,例举一个
class MTransitionTurnOffTiles : TransitionTurnOffTiles{ public: static TransitionScene* create(float duration,Scene* scene){ return TransitionTurnOffTiles::create(duration,scene); } };为了可以来回切换着看这些效果,通过向MainScene中添加不同的layer,每一个layer中有三个按钮,点击按钮的时候,然后构造一个切换效果Scene,进行切换场景,具体看code
LayerOne::LayerOne(){ init(); } LayerOne::~LayerOne(){} bool LayerOne::init(){ if (!Layer::init()) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); MenuItemImage* restart = MenuItemImage::create("res/r1.png","res/r2.png",CC_CALLBACK_1(LayerOne::menuRestartCallback,this)); restart->setPosition(Vec2(visibleSize.width / 2,restart->getContentSize().height)); MenuItemImage* back = MenuItemImage::create("res/b1.png","res/b2.png",CC_CALLBACK_1(LayerOne::menuBackCallback,this)); back->setPosition(Vec2(visibleSize.width / 2 - restart->getContentSize().width*1.5,back->getContentSize().height)); MenuItemImage* forward = MenuItemImage::create("res/f1.png","res/f2.png",CC_CALLBACK_1(LayerOne::menuForwardCallback,this)); forward->setPosition(Vec2(visibleSize.width / 2 + restart->getContentSize().width*1.5,forward->getContentSize().height)); auto menu = Menu::create(restart,back,forward,NULL); menu->setPosition(Vec2::ZERO); this->addChild(menu,1); auto label = Label::createWithTTF("LayerOne","fonts/Marker Felt.ttf",24); label->setPosition(Vec2(origin.x + visibleSize.width / 2,origin.y + visibleSize.height - label->getContentSize().height)); this->addChild(label,1); auto transitionName = Label::createWithTTF(transitions[transition_index].name,24); transitionName->setPosition(Vec2(origin.x + visibleSize.width / 2,origin.y + visibleSize.height - transitionName->getContentSize().height * 2)); this->addChild(transitionName,1); auto sprite = Sprite::create("res/background1.png"); sprite->setPosition(Vec2(visibleSize.width / 2 + origin.x,visibleSize.height / 2 + origin.y)); this->addChild(sprite,0); return true; } void LayerOne::menuRestartCallback(Ref* pSender){ auto go = MainScene::createScene(); Director::getInstance()->replaceScene(go); } void LayerOne::menuBackCallback(Ref* pSender){ transition_index--; if (transition_index < 0){ transition_index += MAX_TRANSITION_COUNT; } Scene* s = MainScene::createScene(); Layer* layer2 = new LayerTwo(); s->addChild(layer2); auto scene = createTranstion(transition_index,DURATION,s); if (scene){ Director::getInstance()->replaceScene(scene); } } void LayerOne::menuForwardCallback(Ref* pSender){ transition_index++; transition_index = transition_index % MAX_TRANSITION_COUNT; Scene* s = MainScene::createScene(); Layer* layer2 = new LayerTwo(); s->addChild(layer2); auto scene = createTranstion(transition_index,s); if (scene){ Director::getInstance()->replaceScene(scene); } }另外一个LayerTwo也是类似,只不过背景图片不同
win上可以运行,Android下,我刚开始不能run as,会报一些宏定义的错误,但是用命令编译过之后就好了
android下效果图:
(后面发现少了点,我补充下)
原文链接:https://www.f2er.com/cocos2dx/344555.html