在上节的基础上,看看怎么实现场景的切换。


添加一个场景WelcomeScene


WelcomeScene.h

#pragma once
#include "cocos2d.h"
       
USING_NS_CC;
       
class WelcomeScene :
    public CCLayer
{
public:
    WelcomeScene(void);
    ~WelcomeScene(void);
          
    virtual bool init();
          
    static CCScene* scene();
          
    CREATE_FUNC(WelcomeScene);
          
    void menuOKCallback(CCObject *pSender);
};


WelcomeScene.cpp


#include "WelcomeScene.h"
#include "GameScene.h"
          
WelcomeScene::WelcomeScene(void){}
WelcomeScene::~WelcomeScene(void){}
          
CCScene* WelcomeScene::scene()
{
    CCScene* scene = CCScene::create();
          
    WelcomeScene* welcomeScene = WelcomeScene::create();
    scene->addChild(welcomeScene);
          
          
    return scene;
}
          
bool WelcomeScene::init()
{
    if(!CCLayer::init())
    {
        return false;
    }
          
    CCSize size = CCDirector::sharedDirector()->getWinSize();
          
    //添加背景
    CCSprite* welcomeBg = CCSprite::create("welcome_bg.png");
    welcomeBg->setPosition(ccp(size.width / 2, size.height / 2));
    this->addChild(welcomeBg, 0);
          
    CCMenuItemImage* okItem = CCMenuItemImage::create(
        "ok_normal.png",
        "ok_pressed.png",
        this,
        menu_selector(WelcomeScene::menuOKCallback)
    );
          
    CCMenu* menu = CCMenu::create(okItem,NULL);
    menu->setAnchorPoint(ccp(1.0f, 0.0f));
    menu->setPosition(ccp(size.width - okItem->getContentSize().width,
        okItem->getContentSize().height));
    this->addChild(menu);
          
    return true;
}
          
void WelcomeScene::menuOKCallback(CCObject *pSender)
{
    CCScene* scene = GameScene::scene();
    CCTransitionFadeTR* txScene =  CCTransitionFadeTR::create(1.2, scene);
          
    CCDirector::sharedDirector()->replaceScene(txScene);
}


网上找的特效:

CCTransitionJumpZoom::create(t, s);//跳跃式,本场景先会缩小,然后跳跃进来
CCTransitionFade::create(t, s);//淡出淡入,原场景淡出,新场景淡入
CCTransitionShrinkGrow::create(t, s);//交错换
CCTransitionRotoZoom::create(t, s);//转角换
CCTransitionMoveInL::create(t, s);//新场景从左移入覆盖
CCTransitionMoveInR::create(t, s);//新场景从右移入覆盖
CCTransitionMoveInT::create(t, s);//新场景从上移入覆盖
CCTransitionMoveInB::create(t, s);//新场景从下移入覆盖
CCTransitionSlideInL::create(t, s);//场景从左移入推出原场景
CCTransitionSlideInR::create(t, s);//场景从右移入推出原场景
CCTransitionSlideInT::create(t, s);//场景从上移入推出原场景
CCTransitionSlideInB::create(t, s);//场景从下移入推出原场景
CCTransitionFadeTR::transitionWithDuration(t, s);//向右上波浪
CCTransitionFadeBL::transitionWithDuration(t, s);//向左下波浪
CCTransitionFadeUp::transitionWithDuration(t, s);//向上百叶窗
CCTransitionFadeDown::transitionWithDuration(t, s);//向下百叶窗
CCTransitionTurnOffTiles::transitionWithDuration(t, s);//随机小方块
CCTransitionSplitRows::transitionWithDuration(t, s);//按行切
CCTransitionSplitCols::transitionWithDuration(t, s);//按列切