首页 > 代码库 > cocos2d-x学习记录6——CCTransition场景过渡
cocos2d-x学习记录6——CCTransition场景过渡
利用CCTransition能够创建出一系列的场景过渡动画,能够使场景切换时更加绚丽丰富。
CCTransition派生出很多过渡动画,传入的参数一般包括过渡时间和创建的场景。
MyScene.h内容
1 #ifndef MyScene_H_H 2 #define MyScene_H_H 3 4 #include "cocos2d.h" 5 using namespace cocos2d; 6 7 8 class TranScene 9 {10 public:11 static CCTransitionScene* createScene(CCScene *scene);12 13 static int index;14 };15 16 17 class MyScene : public CCLayer18 {19 public:20 static CCScene* createScene();21 virtual bool init();22 CREATE_FUNC( MyScene );23 24 virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);25 //virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);26 //virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);27 //virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);28 29 virtual void registerWithTouchDispatcher();30 31 //virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent);32 //virtual void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent);33 //virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent);34 //virtual void ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent);35 36 private:37 };38 39 class NextScene : public CCLayer40 {41 public:42 static CCScene* createScene();43 virtual bool init();44 CREATE_FUNC( NextScene );45 46 virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);47 48 virtual void registerWithTouchDispatcher();49 50 private:51 };52 53 #endif
MyScene.cpp内容
#include "MyScene.h"int TranScene::index = 0;CCTransitionScene* TranScene::createScene(CCScene *scene){ CCTransitionScene *tranScene = NULL; switch(index){ case 0: tranScene = CCTransitionFlipX::create(1, scene); break; case 1: tranScene = CCTransitionFlipY::create(1, scene); break; case 2: tranScene = CCTransitionFadeDown::create(1, scene); break; case 3: tranScene = CCTransitionFadeBL::create(1, scene); break; case 4: tranScene = CCTransitionCrossFade::create(1, scene); break; } index = (++index)%5; return tranScene;}CCScene* MyScene::createScene(){ CCScene *scene = CCScene::create(); MyScene *layer = MyScene::create(); scene->addChild(layer); return scene;};bool MyScene::init(){ if( !CCLayer::init() ){ return false; } CCSize size = CCDirector::sharedDirector()->getWinSize(); CCSprite *sprite = CCSprite::create("pal4.png"); sprite->setAnchorPoint( ccp(0.5, 0.5) ); //sprite->setPosition( ccp(size.width/2, size.height/2) ); sprite->setPosition( ccp(size.width/2, size.height/2) ); sprite->setScaleX( size.width/sprite->getContentSize().width ); sprite->setScaleY( size.height/sprite->getContentSize().height ); sprite->setTag(2345); addChild(sprite); setTouchEnabled(true); return true;}void MyScene::registerWithTouchDispatcher(){ CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);}bool MyScene::ccTouchBegan(CCTouch* pTouch, CCEvent *pEvent){ CCScene *scene = NextScene::createScene(); //CCTransitionScene *tranScene = CCTransitionFlipX::create(1, scene); CCTransitionScene *tranScene = TranScene::createScene(scene); CCDirector::sharedDirector()->replaceScene(tranScene); return true;}CCScene* NextScene::createScene(){ CCScene *scene = CCScene::create(); NextScene *layer = NextScene::create(); scene->addChild(layer); return scene;};bool NextScene::init(){ if( !CCLayer::init() ){ return false; } CCSize size = CCDirector::sharedDirector()->getWinSize(); CCSprite *sprite = CCSprite::create("pal_4.png"); sprite->setAnchorPoint( ccp(0.5, 0.5) ); //sprite->setPosition( ccp(size.width/2, size.height/2) ); sprite->setPosition( ccp(size.width/2, size.height/2) ); //sprite->setScale(0.5f); sprite->setScaleX( size.width/sprite->getContentSize().width ); sprite->setScaleY( size.height/sprite->getContentSize().height ); sprite->setTag(2345); addChild(sprite); setTouchEnabled(true); return true;}void NextScene::registerWithTouchDispatcher(){ CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);}bool NextScene::ccTouchBegan(CCTouch* pTouch, CCEvent *pEvent){ CCScene *scene = MyScene::createScene(); //CCTransitionScene *tranScene = CCTransitionFlipX::create(1, scene); CCTransitionScene *tranScene = TranScene::createScene(scene); CCDirector::sharedDirector()->replaceScene(tranScene); return true;}
实例中,自己简单创建了两个场景,仅仅是所显示的sprite不同而已,并在切换时添加具体的切换效果即可。
运行效果:
cocos2d-x内部封装了较多过渡效果,需要时自己选取即可。
cocos2d-x学习记录6——CCTransition场景过渡
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。