步骤:
1.新建一个Cocos2d-x项目
2.在include中添加SecondScene.h文件,添加代码
3.在source中添加SecondScene.cpp文件,添加代码
4.在HelloWorldScene.cpp中的增加和修改代码
1.新建一个Cocos2d-x项目并运行,保证可以运行起来
2.在include中添加SecondScene.h文件,添加代码
#include "cocos2d.h" using namespace cocos2d; class SecondScene : public cocos2d::CCScene { public: virtual bool init(); CREATE_FUNC(SecondScene); static cocos2d::CCScene* scene(); public: CCLayer *m_layer; };
3.在source中添加SecondScene.cpp文件,添加代码
#include "SecondScene.h" using namespace cocos2d; bool SecondScene::init() { bool bRet = false; do { //实例化精灵 CCSprite *m_sprite = CCSprite::create("CloseNormal.png"); CC_BREAK_IF(!m_sprite); //设置精灵位置 m_sprite->setPosition(ccp(100, 100)); //添加精灵到场景 this->addChild(m_sprite); bRet = true; } while (0); return bRet; } CCScene* SecondScene::scene() { SecondScene *m_secondScene = NULL; do { //实例化场景 m_secondScene = SecondScene::create(); CC_BREAK_IF(!m_secondScene); //实例化层 CCLayer *m_layer = CCLayer::create(); CC_BREAK_IF(! m_layer); //将层添加到场景中 m_secondScene->addChild(m_layer); } while (0); return m_secondScene; }
4.在HelloWorldScene.cpp中的增加和修改代码
加入代码:
#include "SecondScene.h"
修改HelloWorld::menuCloseCallback(CCObject* pSender)函数
void HelloWorld::menuCloseCallback(CCObject* pSender) { // "close" menu item clicked //CCDirector::sharedDirector()->end(); CCScene *secondScene = SecondScene::scene(); CCDirector::sharedDirector()->replaceScene(CCTransitionJumpZoom::create(1.0f,secondScene)); }
运行程序,点击右下角的按钮即可实现场景跳转,主要的场景跳转代码是:
CCDirector::sharedDirector()->replaceScene(CCTransitionJumpZoom::create(1.0f,secondScene));
其中CCTransitionJumpZoom::create(1.0f,secondScene)实现特效跳转