標籤:style blog http color io os ar 檔案 2014
1 class BaseTest : public cocos2d::Layer 2 { 3 public: 4 virtual std::string title() const;// 主標題 5 virtual std::string subtitle() const;// 副標題 6 7 virtual void restartCallback(Ref* sender);// 重設情境回調 8 virtual void nextCallback(Ref* sender);// 下一個情境回調 9 virtual void backCallback(Ref* sender);// 上一個情境回調10 11 virtual void onEnter() override;// 進入層時12 virtual void onExit() override;// 退出層時13 };
上面就是BaseTest類的標頭檔,裡面只要提供了測試每個情境時用於情境的切換,其實就是測試項的切換,同時也是介面類,我們來看一下:
接下我們來看一下實現BaseTest.cpp檔案:
1 void BaseTest::onEnter() 2 { 3 Layer::onEnter(); 4 // ... 5 // 添加主標題 6 std::string str = title(); 7 const char * pTitle = str.c_str(); 8 9 TTFConfig ttfConfig("fonts/arial.ttf", /* 字型大小 */32);10 auto label = Label::createWithTTF(ttfConfig,pTitle);11 addChild(label, 9999);12 label->setPosition( Vec2(VisibleRect::center().x, VisibleRect::top().y - 30) );13 // 添加副標題14 std::string strSubtitle = subtitle();15 if( ! strSubtitle.empty() )16 {17 ttfConfig.fontFilePath = "fonts/Thonburi.ttf";// 字型檔路徑18 ttfConfig.fontSize = 16;// 字型字型大小19 auto l = Label::createWithTTF(ttfConfig,strSubtitle.c_str());20 addChild(l, 9999);21 l->setPosition( Vec2(VisibleRect::center().x, VisibleRect::top().y - 60) );22 }23 // 添加菜單圖片項24 auto item1 = MenuItemImage::create(s_pathB1, s_pathB2, CC_CALLBACK_1(BaseTest::backCallback, this) );25 auto item2 = MenuItemImage::create(s_pathR1, s_pathR2, CC_CALLBACK_1(BaseTest::restartCallback, this) );26 auto item3 = MenuItemImage::create(s_pathF1, s_pathF2, CC_CALLBACK_1(BaseTest::nextCallback, this) );27 28 auto menu = Menu::create(item1, item2, item3, nullptr);29 // 設定位置30 menu->setPosition(Vec2::ZERO);31 item1->setPosition(Vec2(VisibleRect::center().x - item2->getContentSize().width*2, VisibleRect::bottom().y+item2->getContentSize().height/2));32 item2->setPosition(Vec2(VisibleRect::center().x, VisibleRect::bottom().y+item2->getContentSize().height/2));33 item3->setPosition(Vec2(VisibleRect::center().x + item2->getContentSize().width*2, VisibleRect::bottom().y+item2->getContentSize().height/2));34 35 addChild(menu, 9999);36 }
上面的函數當進入層時調用可以重寫,主要是顯示主要標題,副標題,切換箭頭表徵圖,重設表徵圖,至於其他嘛就沒什麼了。
1 /* 層退出時調用 */2 void BaseTest::onExit()3 {4 // ... 5 Layer::onExit();6 }
這個就不用說了,可以重寫。
1 /* 這四個函數主要是繼承時要進行重寫 */ 2 std::string BaseTest::title() const 3 { 4 return ""; 5 } 6 7 std::string BaseTest::subtitle() const 8 { 9 return "";10 }11 12 void BaseTest::restartCallback(Ref* sender)13 {14 log("override restart!");15 }16 17 void BaseTest::nextCallback(Ref* sender)18 {19 log("override next!");20 }21 22 void BaseTest::backCallback(Ref* sender)23 {24 log("override back!");25 }
這四個函數擺明就是要重寫的,因為這個類是提供介面類,所以至於怎麼切換還是要看後面怎麼重寫了。
這個總的來說,就是為其他類提供情境的切換功能。
Cocos2d-x_3.2 Demo ----------BaseTestt類