cocos2dx 3.x 场景切换特效整合——实现一个场景工厂管理类

用了coco2dx引擎也有段时间了,但是总感觉不是很好用,为了写一个功能,往往要写好几行代码,就比如做一个场景切换特效,就要这么写

auto scene=HelloWorld::create();//创建待切换的场景
auto transition=TransitionFade::create(1.0f,scene);//给场景增加特效 
Director::getInstance()->replaceScene(transition);//运用导演类来进行切换场景

简单的一个功能,却要写三行代码,而且很多地方需要用到场景切换,每次使用就要至少写三行,还要做添加不同场景对应头文件之类的事,真的好麻烦。
作为一个讲究效率(其实是嫌麻烦)的程序员,肯定想尽可能的避免这类麻烦的事发生。如果能进一步封装一下这三行代码,直接一个函数就实现场景切换特效该多好,于是乎,一个场景工厂管理类就诞生了,这可以大大的提高我的开发效率(你说这人怎么这么懒呢)。
不多说了,上代码。
首先是SceneFactory.h

#ifndef __SceneFactory_h__
#define __SceneFactory_h__
#include "cocos2d.h"
//scene headers
#include "GameScene.h"
#include "LoginScene.h"
#include "IntroScene.h"



USING_NS_CC;
//场景类型,根据自己的项目场景修改和添加
enum kSceneType
{
    kSceneGame = 1,
    kSceneChallenge,
    kSceneIntro,
    kSceneLogin
};

//场景切换效果类型
enum kTranslateType
{
    kNone,//没有任何效果
    kCrossFade,//淡出当前场景的同时淡入下一个场景
    kFade,//淡出当前场景到指定颜色,默认颜色为c3b(0,0,0),可用最后一个参数指定颜色
    kFadeBL,//从左下角开始淡出场景
    kFadeDown,//从底部开始淡出场景
    kFadeTR,//从右上角开始淡出场景
    kFadeUp,//从顶部开始淡出场景
    kFlipAngularLeftOver,//当前场景倾斜后翻转成下一个场景,默认从左边开始翻转
    kFlipAngularRightOver,//当前场景倾斜后翻转成下一个场景,默认从左边开始翻转
    //LeftOver,//从左边开始
    //RightOver,//从右边开始
    //UpOver,//从顶部开始
    //DownOver,//从底部开始
    //kFlipX,//水平翻转,默认从左往右翻转,可用的附加参数同上
    kFlipXLeftOver,
    kFlipXRightOver,
    kFlipXUpOver,
    kFlipXDownOver,
    //kFlipY,//垂直翻转,默认从上往下翻转,可用的附加参数同上
    kFlipYLeftOver,
    kFlipYRightOver,
    kFlipYUpOver,
    kFlipYDownOver,
    kZoomFlipAngularLeftOver,//倾斜翻转的同时放大,可用的附加参数同上
    kZoomFlipAngularRightOver,//倾斜翻转的同时放大,可用的附加参数同上
    //kZoomFlipX,//水平翻转的同时放大,可用的附加参数同上 
    kZoomFlipXLeftOver,
    kZoomFlipXRightOver,
    kZoomFlipXUpOver,
    kZoomFlipXDownOver,
    //kZoomFlipY,//垂直翻转的同时放大,可用的附加参数同上
    kZoomFlipYLeftOver,
    kZoomFlipYRightOver,
    kZoomFlipYUpOver,
    kZoomFlipYDownOver,

    kJumpZoom,//跳跃放大切换场景
    kMoveInB,//新场景从底部进入,直接覆盖现有场景
    kMoveInL,//新场景从左侧进入,直接覆盖现有场景
    kMoveInR,//新场景从右侧进入,直接覆盖现有场景
    kMoveInT,//新场景从顶部进入,直接覆盖现有场景
    kPageTurnTure,//翻页效果,如果指定附加参数为,//true,则表示从左侧往右翻页
    kPageTurnFalse,//翻页效果,如果指定附加参数为,//true,则表示从左侧往右翻页
    kRotoZoom,//旋转放大切换场景
    kShrinkGrow,//收缩交叉切换场景
    kSlideInB,//新场景从底部进入,现有场景同时从顶部退出
    kSlideInL,//新场景从左侧进入,现有场景同时从右侧退出
    kSlideInR,//新场景从右侧进入,现有场景同时从左侧退出
    kSlideInT,//新场景从顶部进入,现有场景同时从底部退出
    kSplitCols,//分成多列切换入新场景
    kSplitRows,//分成多行切换入新场景,类似百叶窗
    kTurnOffTiles//当前场景分成多个块,逐渐替换为新场景
};

class SceneFactory {
public:
    SceneFactory();
    ~SceneFactory();

    //新建一个场景
    static Scene* create(kSceneType type);
    //新建一个带参数的场景
    static Scene* create(kSceneType type, int level);
    //场景切换(场景类型,场景切换类型,切换所需时间)
    static void changeScene(kSceneType type, kTranslateType transType, float t);
    //场景切换(场景类型,带自定义参数的场景,场景切换类型,切换所需时间)
    static void changeScene(kSceneType type, int level, kTranslateType transType, float t);

    static TransitionScene* createTransition(kTranslateType transType, Scene* s, float t);
private:

};

#endif // SceneFactory_h__

然后是其对应的SceneFactory.cpp

#include "SceneFactory.h"
Scene* SceneFactory::create(kSceneType type)
{
    Scene* scene = nullptr;
    //该工厂根据场景的类型创造出相对应的场景
    switch (type)
    {
    case kSceneGame:
        scene = GameScene::create();
        break;
    case kSceneChallenge:
        break;
    case kSceneIntro:
        scene = IntroScene::create();
        break;
    case kSceneLogin:
        scene = LoginScene::create();
        break;
    default:
        break;
    }
    return scene;
}

Scene* SceneFactory::create(kSceneType type, int level)
{
    Scene* scene = nullptr;
    switch (type)
    {
    case kSceneGame:
        scene = GameScene::create(level);
        break;
    default:
        break;
    }
    return scene;
}

TransitionScene* SceneFactory::createTransition(kTranslateType transType, Scene* s, float t)
{
    TransitionScene* scene = nullptr;
    //根据场景的转换类型,生产出不同的带特效的转换场景
    //这些场景转换特效我是从cpp-test里的场景转换的例子里抄过来的,如果有缺少,请按照格式自行添加。
    switch (transType)
    {
    case kNone:
        scene = TransitionScene::create(t, s);
        break;
    case kCrossFade:
        scene = TransitionCrossFade::create(t, s);
        break;
    case kFade:
        scene = TransitionFade::create(t, s);
        break;
    case kFadeBL:
        scene = TransitionFadeBL::create(t, s);
        break;
    case kFadeDown:
        scene = TransitionFadeDown::create(t, s);
        break;
    case kFadeTR:
        scene = TransitionFadeTR::create(t, s);
        break;
    case kFadeUp:
        scene = TransitionFadeUp::create(t, s);
        break;
    case kFlipAngularLeftOver:
        scene = TransitionFlipAngular::create(t, s, TransitionScene::Orientation::LEFT_OVER);
        break;
    case kFlipAngularRightOver:
        scene = TransitionFlipAngular::create(t, s, TransitionScene::Orientation::RIGHT_OVER);
        break;
    case kFlipXLeftOver:
        scene = TransitionFlipX::create(t, s, TransitionScene::Orientation::LEFT_OVER);
        break;
    case kFlipXRightOver:
        scene = TransitionFlipX::create(t, s, TransitionScene::Orientation::RIGHT_OVER);
        break;
    case kFlipXUpOver:
        scene = TransitionFlipX::create(t, s, TransitionScene::Orientation::UP_OVER);
        break;
    case kFlipXDownOver:
        scene = TransitionFlipX::create(t, s, TransitionScene::Orientation::DOWN_OVER);
        break;
    case kFlipYLeftOver:
        scene = TransitionFlipY::create(t, s, TransitionScene::Orientation::LEFT_OVER);
        break;
    case kFlipYRightOver:
        scene = TransitionFlipY::create(t, s, TransitionScene::Orientation::RIGHT_OVER);
        break;
    case kFlipYUpOver:
        scene = TransitionFlipY::create(t, s, TransitionScene::Orientation::UP_OVER);
        break;
    case kFlipYDownOver:
        scene = TransitionFlipY::create(t, s, TransitionScene::Orientation::DOWN_OVER);
        break;
    case kZoomFlipAngularLeftOver:
        scene = TransitionZoomFlipAngular::create(t, s, TransitionScene::Orientation::LEFT_OVER);
        break;
    case kZoomFlipAngularRightOver:
        scene = TransitionZoomFlipAngular::create(t, s, TransitionScene::Orientation::RIGHT_OVER);
        break;
    case kZoomFlipXLeftOver:
        scene = TransitionZoomFlipX::create(t, s, TransitionScene::Orientation::LEFT_OVER);     
        break;
    case kZoomFlipXRightOver:
        scene = TransitionZoomFlipX::create(t, s, TransitionScene::Orientation::RIGHT_OVER);
        break;
    case kZoomFlipXUpOver:
        scene = TransitionZoomFlipX::create(t, s, TransitionScene::Orientation::UP_OVER);
        break;
    case kZoomFlipXDownOver:
        scene = TransitionZoomFlipX::create(t, s, TransitionScene::Orientation::DOWN_OVER);
        break;
    case kZoomFlipYLeftOver:
        scene = TransitionZoomFlipY::create(t, s, TransitionScene::Orientation::LEFT_OVER);
        break;
    case kZoomFlipYRightOver:
        scene = TransitionZoomFlipY::create(t, s, TransitionScene::Orientation::RIGHT_OVER);
        break;
    case kZoomFlipYUpOver:
        scene = TransitionZoomFlipY::create(t, s, TransitionScene::Orientation::UP_OVER);
        break;
    case kZoomFlipYDownOver:
        scene = TransitionZoomFlipY::create(t, s, TransitionScene::Orientation::DOWN_OVER);     
        break;
    case kJumpZoom:
        scene = TransitionJumpZoom::create(t, s);
        break;
    case kMoveInB:
        scene = TransitionMoveInB::create(t, s);
        break;
    case kMoveInL:
        scene = TransitionMoveInL::create(t, s);
        break;
    case kMoveInR:
        scene = TransitionMoveInR::create(t, s);
        break;
    case kMoveInT:
        scene = TransitionMoveInT::create(t, s);
        break;
    case kPageTurnTure:
        scene = TransitionPageTurn::create(t, s,true);
        break;
    case kPageTurnFalse:
        scene = TransitionPageTurn::create(t, s, false);
        break;
    case kRotoZoom:
        scene = TransitionRotoZoom::create(t, s);
        break;
    case kShrinkGrow:
        scene = TransitionShrinkGrow::create(t, s);
        break;
    case kSlideInB:
        scene = TransitionSlideInB::create(t, s);
        break;
    case kSlideInL:
        scene = TransitionSlideInL::create(t, s);
        break;
    case kSlideInR:
        scene = TransitionSlideInR::create(t, s);
        break;
    case kSlideInT:
        scene = TransitionSlideInT::create(t, s);
        break;
    case kSplitCols:
        scene = TransitionSplitCols::create(t, s);
        break;
    case kSplitRows:
        scene = TransitionSplitRows::create(t, s);
        break;
    case kTurnOffTiles:
        scene = TransitionTurnOffTiles::create(t, s);
        break;
    default:
        break;
    }
    return scene;
}

void SceneFactory::changeScene(kSceneType type, kTranslateType transType, float t)
{
    auto scene = SceneFactory::create(type);
    auto transcene = SceneFactory::createTransition(transType,scene,t);

    //若当前有正在运行的场景,切换成该新场景,否则运行该新场景
    if (Director::getInstance()->getRunningScene()) { if (transType == kNone) { Director::getInstance()->replaceScene(scene);
        }
        else
        {
            Director::getInstance()->replaceScene(transcene);
        }

    }
    else
    {
        Director::getInstance()->runWithScene(scene);
    }
}

void SceneFactory::changeScene(kSceneType type, int level, kTranslateType transType, float t)
{
    auto scene = SceneFactory::create(type,level);
    auto transcene = SceneFactory::createTransition(transType, scene, t);
    if (Director::getInstance()->getRunningScene()) { Director::getInstance()->replaceScene(transcene);
    }
    else
    {
        Director::getInstance()->runWithScene(scene);
    }
}



SceneFactory::SceneFactory()
{
}

SceneFactory::~SceneFactory()
{
}

好啦,以上就是所有代码(好多好多场景切换特效,写的有点累,不过为了以后能偷懒,我忍),那怎么使用呢?很简单。

SceneFactory::changeScene(kTypeGame,kFade,0.3f);

只要这么一行代码,就通过Fade(其实我也不知道是什么特效)的方式切换到你的GameScene了。当然头文件还是免不了的,但是你不用担心用哪个场景的头文件了,因为你只要用SceneFactory.h就可以了。

#define SceneFactory SF

(其实你可以更懒的)。

你可能感兴趣的:(工厂模式,cocos2d-x,场景切换特效)