場景切換
來源:程序員人生 發布時間:2014-10-03 08:00:01 閱讀次數:1904次
實現場景轉換需要通過場景函數實現,場景函數有:
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函數切換場景的時候會實現數據累加
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈