实现场景转换需要通过场景函数实现,场景函数有:
runWithScene:运行场景
runWithScene属于CCDirector类的成员函数
函数原型: void runWithScene(CCScene *pScene)
函数参数:
*pScene(CCScene型指针)
replaceScene:切换场景
replaceScene属于CCDirector类的成员函数
函数原型: void replaceScene(CCScene *pScene);
函数参数:
*pScene(CCScene型指针)
pushScene: 将场景压到栈中
pushScene属于CCDirector类的成员函数
函数原型:void pushScene(CCScene *pScene);
函数参数:
*pScene(CCScene型指针)
popScene :弹出栈中的场景
popScene属于CCDirector类的成员函数
函数原型:void popScene(void);
函数参数:
没有参数
pause: 暂停场景
pause属于CCDirector类的成员函数
函数原型:void pause(void)
函数参数:
没有参数
resume:继续运行场景
resume属于CCDirector类的成员函数
函数原型:void resume(void)
函数参数:
没有参数
end: 结束场景
end属于CCDirector类的成员函数
函数原型:void end(void)
函数参数:
没有参数
建立工程测试场景函数:
建立一个SceneMan工程,在工程中添加一个SceneSecond类
在SceneSecond.h文件中添加下面的代码
#ifndef _SenceSecond_H_ #define _SenceSecond_H_ //防止代码重包含 #include "cocos2d.h" USING_NS_CC; class SceneSecond : public CCLayer { public: //创建一个场景 static CCScene* scene(); //初始化场景 bool init(); //菜单回调函数 void menuCloseCallback(CCObject* pSender); CREATE_FUNC(SceneSecond); }; #endif
在SeceneSecond.cpp中添加下面的代码
#include "SceneSecond.h" #include "HelloWorldScene.h" CCScene* SceneSecond::scene() { //创建一个场景 CCScene* s = CCScene::create(); //创建一个layer SceneSecond* layer = SceneSecond::create(); //将layer加到场景中 s->addChild(layer); //返回CCScene指针 return s; } bool SceneSecond::init() { //先调用父类的init函数 CCLayer::init(); //在场景中显示文字 CCLabelTTF *label = CCLabelTTF::create("SceneSecond", "abcde", 36); //设置显示文字的位置(坐标) label->setPosition(CCPoint(100,200)); //将文字加到场景中 addChild(label); CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(SceneSecond::menuCloseCallback)); pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 , origin.y + pCloseItem->getContentSize().height/2)); //创建一个菜单 CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu, 1); return true; } //菜单回调函数 void SceneSecond::menuCloseCallback(CCObject* pSender) { //切换到HelloWorld场景 CCDirector::sharedDirector()->popScene(); }
最后将HelloWorld.cpp文件中的代码修改成
#include "HelloWorldScene.h" #include "SceneSecond.h" USING_NS_CC; CCScene* HelloWorld::scene() { // 'scene' is an autorelease object CCScene *scene = CCScene::create(); // 'layer' is an autorelease object HelloWorld *layer = HelloWorld::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene; } // on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); ///////////////////////////// // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. // add a "close" icon to exit the progress. it's an autorelease object CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback)); pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 , origin.y + pCloseItem->getContentSize().height/2)); // create menu, it's an autorelease object CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu, 1); static int i = 1; char buf[256]; sprintf(buf, "Hello World %d", i++); CCLabelTTF* pLabel = CCLabelTTF::create(buf, "Arial", 24); // position the label on the center of the screen pLabel->setPosition(ccp(origin.x + visibleSize.width/2, origin.y + visibleSize.height - pLabel->getContentSize().height)); // add the label as a child to this layer this->addChild(pLabel, 1); // add "HelloWorld" splash screen" CCSprite* pSprite = CCSprite::create("HelloWorld.png"); // position the sprite on the center of the screen pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); // add the sprite as a child to this layer this->addChild(pSprite, 0); return true; } //点击按钮后调用的函数 void HelloWorld::menuCloseCallback(CCObject* pSender) { //切换到SceneSecond场景 CCDirector::sharedDirector()->pushScene(SceneSecond::scene()); }
编译程序:程序的执行结果为
修改程序代码,将SceneSecond.cpp中的void SceneSecond::menuCloseCallback(CCObject* pSender)函数中的代码
CCDirector::sharedDirector()->popScene();
修改成
CCDirector::sharedDirector()->replaceScene(HelloWorld::scene());
将HelloWorldScene.cpp中void HelloWorld::menuCloseCallback(CCObject* pSender)函数中的代码
CCDirector::sharedDirector()->pushScene(SceneSecond::scene());
修改成
CCDirector::sharedDirector()->replaceScene(SceneSecond::scene());
程序执行结果:
修改后HelloWorld后的数字会出现累加,是因为在上一个程序中使用的场景切换函数为栈函数,在切换场景的过程中会执行出栈和压栈操作,不会出现累加,而使用replace函数切换场景的时候会实现数据累加