Cocos2d-x文本菜單
文本菜單是菜單項隻是顯示文本文本菜單類包括了MenuItemLabel、MenuItemFont和MenuItemAtlasFont。MenuItemLabel是個抽象類具體使用的時候是使用MenuItemFont和MenuItemAtlasFont兩個類。
文本菜單類MenuItemFont它的其中一個創建函數create定義如下
static MenultemAtlasFont*create ( const std::string & value, //要顯示的文本 const ccMenuCallback & callback //菜單操作的回調函數指針 )
文本菜單類MenuItemAtlasFont是基於圖片集的文本菜單項它的其中一個創建函數create定義如下
static MenuItemAtlasFont* create ( const std::string & value, //要顯示的文本 const std::string & charMapFile, //圖片集合文件 int itemWidth, //要截取的文字在圖片中的寬度 int itemHeight, //要截取的文字在圖片中的高度 char startCharMap //菜單操作的回調函數指針 )
這次我們會通過一個實例介紹一下文本菜單的使用這個實例如下圖所示其中菜單Start是使用MenuItemFont實現的菜單Help是使用MenuItemAtlasFont實現的。
下麵我們看看HelloWorldScene.cpp中init函數如下
bool HelloWorld::init() { if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->getVisibleOrigin(); Sprite *bg =Sprite::create("menu/background.png"); bg->setPosition(Point(origin.x + visibleSize.width/2, origin.y +visibleSize.height /2)); this->addChild(bg); MenuItemFont::setFontName("Times New Roman"); ① MenuItemFont::setFontSize(86); ② MenuItemFont *item1 = MenuItemFont::create("Start", CC_CALLBACK_1(HelloWorld::menuItem1Callback,this)); ③ MenuItemAtlasFont *item2 = MenuItemAtlasFont::create("Help", "menu/tuffy_bold_italic-charmap.png",48, 65, ' ', CC_CALLBACK_1(HelloWorld::menuItem2Callback,this)); ④ Menu* mn = Menu::create(item1, item2, NULL); ⑤ mn->alignItemsVertically(); ⑥ this->addChild(mn); ⑦ return true; }
上述代碼第①和②行是設置文本菜單的文本字體和字體大小。第③行代碼是創建MenuItemFont菜單項對象它是一個一般文本菜單create是函數的第一個參數是菜單項的文本內容第二個參數是點擊菜單項回調的函數指針。其中CC_CALLBACK_1宏是定義一個回調函數並函數與對象綁定在一起1表示這個函數有一個輸出參數HelloWorld::menuItem1Callback是函數指針this代表函數所在的對象。
HelloWorld::menuItem1Callback需要在HelloWorld.h頭文件中聲明HelloWorld.h頭文件代碼如下
#include "cocos2d.h" class HelloWorld : public cocos2d::Layer { public: virtual bool init(); static cocos2d::Scene* scene(); void menuItem1Callback(cocos2d::Ref*pSender); void menuItem2Callback(cocos2d::Ref*pSender); CREATE_FUNC(HelloWorld); };
回調函數代碼如下函數中的參數是菜單項MenuItem的實例。
void HelloWorld::menuItem1Callback(Ref*pSender) { MenuItem* item = (MenuItem*)pSender; log("TouchStart Menu Item %p", item); } void HelloWorld::menuItem2Callback(Ref*pSender) { MenuItem* item = (MenuItem*)pSender; log("TouchHelp Menu Item %p", item); }
HelloWorldScene.cpp中init函數中第④行代碼是創建一個MenuItemAtlasFont菜單項對象這種菜單項是基於圖片集的菜單項。MenuItemAtlasFont需要將圖片集放到資源目錄Resources下。在本例中我們是將所有的圖片都放到一個Resources下的menu目錄中所以create函數的第二個參數是"menu/tuffy_bold_italic-charmap.png"要求帶有menu路徑。
還有第⑤行代碼Menu* mn = Menu::create(item1, NULL)是創建菜單對象把之前創建的菜單項添加到菜單中create函數中有是這些菜單項的數組最後要用NULL結束。第⑥行代碼mn->alignItemsVertically()是設置菜單項垂直對齊。第⑦行代碼是this->addChild(mn,1,2)是把菜單對象添加到當前層中。
、
《Cocos2d-x實戰 C++卷》現已上線各大商店均已開售
京東https://item.jd.com/11584534.html
當當https://product.dangdang.com/23606265.html
互動出版網https://product.china-pub.com/3770734
《Cocos2d-x實戰 C++卷》源碼及樣章下載地址
源碼下載地址https://51work6.com/forum.php?mod=viewthread&tid=1155&extra=page%3D1
樣章下載地址https://51work6.com/forum.php?mod=viewthread&tid=1157&extra=page%3D1

最後更新:2017-04-03 12:56:38