多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > 互聯網 > 場景切換

場景切換

來源:程序員人生   發布時間: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函數切換場景的時候會實現數據累加



生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 黑人干中国妞 | 伊人影院在线视频 | 日本亚洲黄色 | 欧美黑人巨大videos极品视频 | 中文字幕亚洲综合精品一区 | 亚洲精品久久久久影院 | 久久国产三级 | 中文字幕乱码人成乱码在线视频 | 亚洲天堂久久久 | 免费网站h | 国产在线视频一区 | 亚洲毛片在线观看 | 2020年国产一国产一级毛卡片 | 东京干福利 | 亚洲综合春色另类久久 | 永久免费在线观看视频 | 久久国产亚洲欧美日韩精品 | 亚洲日本中文字幕在线 | 亚洲欧美自拍视频 | 性欧美黑人 | 中文字幕在线观看网址 | 91精品国产一区二区三区左线 | 在线91色 | 国内小情侣一二三区在线视频 | 99久久综合狠狠综合久久aⅴ | 最近无中文字幕视频 | 亚洲午夜色 | 手机看片国产免费 | 国产精品一区二区久久精品 | 精品91| 国产成人精品在视频 | 国产福利不卡视频在免费播放 | 成人在线精品视频 | 国产高清日韩 | 亚洲日韩欧美一区二区在线 | 国产精品嫩草影院在线观看免费 | 午夜手机福利视频 | 欧美亚洲尤物久久精品 | 成人在线视频国产 | 成年人网站在线观看视频 | 性新婚a大黄毛片 |