在上节的基础上,看看怎么实现场景的切换。
添加一个场景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); }
网上找的特效: