Cocos2d-x Learning Record 6 -- Transition of cctransition scenario

Source: Internet
Author: User
Tags addchild

With cctransition, you can create a series of scene transition animations to make scenario switching more colorful.

Cctransition derives many transition animations. The input parameters generally include the transition time and creation scenario.

 

Myscene. H Content

 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 content

#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;}

In the instance, you have created two simple scenarios, only the sprite displayed is different, and you just need to add the specific switching effect when switching.

Running effect:

Cocos2d-x contains a lot of transition effect, you need to select your own.

Cocos2d-x Learning Record 6 -- Transition of cctransition scenario

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.