Classes for creating menus in Cocos2d-X:
Ccmenuitemfont: Create a menu item in plain text
Ccmenuitematlasfont: Create menu items with art Fonts
Ccmenuitemimage: Create a menu item with an image
Ccmenuitemlabel: Creates a menu item with a label.
Ccmenuitemsprite: Use a wizard to create a menu item
Ccmenuitemtoggle: Create a menu item that can be switched back and forth, such as on/off
Program instance: Create a plain text menu item
Program code:
#include "MenuItem.h"CCScene* MenuItem::scene(){ CCScene* scene = CCScene::create(); MenuItem* layer = MenuItem::create(); scene->addChild(layer); return scene;}bool MenuItem::init(){ //初始化父类层 CCLayer::init(); //得到窗口的尺寸 CCSize winSize = CCDirector::sharedDirector()->getWinSize(); //创建一个Menu(看不到效果,Menu只是一个容器) CCMenu* menu = CCMenu::create(); //添加菜单 addChild(menu); //创建一个只显示文本的菜单项 CCMenuItemFont* itemFont = CCMenuItemFont::create("This is Font"); //添加菜单项 menu->addChild(itemFont); return true;}
Execution result:
Program instance: Create a menu item with artistic words
First, add a PNG Image with artistic words under the project directory.
Add the following code
#include "MenuItem.h"CCScene* MenuItem::scene(){ CCScene* scene = CCScene::create(); MenuItem* layer = MenuItem::create(); scene->addChild(layer); return scene;}bool MenuItem::init(){ //初始化父类层 CCLayer::init(); //得到窗口的尺寸 CCSize winSize = CCDirector::sharedDirector()->getWinSize(); //创建一个Menu(看不到效果,Menu只是一个容器) CCMenu* menu = CCMenu::create(); //添加Menu addChild(menu); //创建一个带有艺术字体的菜单 CCMenuItemAtlasFont* itemAtlas = CCMenuItemAtlasFont::create("123456", "labelatlasimg.png", 24, 32, '0'); //添加菜单项 menu->addChild(itemAtlas); return true;}
Execution result:
Program instance: Create a menu item with an image
I am using the pictures that come with the program when creating a project.
Program code
#include "MenuItem.h"CCScene* MenuItem::scene(){ CCScene* scene = CCScene::create(); MenuItem* layer = MenuItem::create(); scene->addChild(layer); return scene;}bool MenuItem::init(){ //初始化父类层 CCLayer::init(); //得到窗口的尺寸 CCSize winSize = CCDirector::sharedDirector()->getWinSize(); //创建一个Menu(看不到效果,Menu只是一个容器) CCMenu* menu = CCMenu::create(); //添加Menu addChild(menu); //创建带有图片的菜单项 CCMenuItemImage* itemImage = CCMenuItemImage::create("CloseNormal.png", "CloseSelected.png"); //添加菜单项 menu->addChild(itemImage); return true;}
Execution result:
Program instance: use tags to create menu items
Program code:
#include "MenuItem.h"CCScene* MenuItem::scene(){ CCScene* scene = CCScene::create(); MenuItem* layer = MenuItem::create(); scene->addChild(layer); return scene;}bool MenuItem::init(){ //初始化父类层 CCLayer::init(); //得到窗口的尺寸 CCSize winSize = CCDirector::sharedDirector()->getWinSize(); //创建一个Menu(看不到效果,Menu只是一个容器) CCMenu* menu = CCMenu::create(); //添加Menu addChild(menu); //用标签创建菜单项 CCMenuItemLabel* itemLabelTTF = CCMenuItemLabel::create(CCLabelTTF::create("This is Label TTF item", "Arial", 36)); //添加菜单项目 menu->addChild(itemLabelTTF); return true;}
Execution result:
Program example: use tags to create menu items and implement color text
First, add a PNG image to the resource folder under the project directory.
Program code
#include "MenuItem.h"CCScene* MenuItem::scene(){ CCScene* scene = CCScene::create(); MenuItem* layer = MenuItem::create(); scene->addChild(layer); return scene;}bool MenuItem::init(){ //初始化父类层 CCLayer::init(); //得到窗口的尺寸 CCSize winSize = CCDirector::sharedDirector()->getWinSize(); //创建一个Menu(看不到效果,Menu只是一个容器) CCMenu* menu = CCMenu::create(); //添加Menu addChild(menu); //用标签创建菜单项,并且实现显示彩色的文本 CCMenuItemLabel* itemLabelAtlas = CCMenuItemLabel::create(CCLabelAtlas::create("123456", "labelatlasimg.png", 24, 32, '0')); //添加菜单项 menu->addChild(itemLabelAtlas); return true;}
Execution result:
Program example: use tags to create menu items and implement color text 2
First, add a font file in fnt format to the resource folder under the project directory.
Program code
#include "MenuItem.h"CCScene* MenuItem::scene(){ CCScene* scene = CCScene::create(); MenuItem* layer = MenuItem::create(); scene->addChild(layer); return scene;}bool MenuItem::init(){ //初始化父类层 CCLayer::init(); //得到窗口的尺寸 CCSize winSize = CCDirector::sharedDirector()->getWinSize(); //创建一个Menu(看不到效果,Menu只是一个容器) CCMenu* menu = CCMenu::create(); //添加Menu addChild(menu); //用艺术字体创建菜单项 CCMenuItemLabel* itemLabelBMFont = CCMenuItemLabel::create(CCLabelBMFont::create("This is BMFONT item", "bitmapFontTest.fnt")); //添加菜单项menu->addChild(itemLabelBMFont); return true;}
Execution result
Program instance: Use the Wizard to create a menu item
The created genie uses the built-in images of the project.
Program code
#include "MenuItem.h"CCScene* MenuItem::scene(){ CCScene* scene = CCScene::create(); MenuItem* layer = MenuItem::create(); scene->addChild(layer); return scene;}bool MenuItem::init(){ //初始化父类层 CCLayer::init(); //得到窗口的尺寸 CCSize winSize = CCDirector::sharedDirector()->getWinSize(); //创建一个Menu(看不到效果,Menu只是一个容器) CCMenu* menu = CCMenu::create(); //添加Menu addChild(menu); //用精灵创建菜单项 CCMenuItemSprite* itemSprite = CCMenuItemSprite::create( CCSprite::create("CloseNormal.png"), CCSprite::create("CloseSelected.png")); //添加菜单项 menu->addChild(itemSprite); return true;}
Execution result:
Program instance: Create menu items that can be switched back and forth, such as on/off
Program code
#include "MenuItem.h"CCScene* MenuItem::scene(){ CCScene* scene = CCScene::create(); MenuItem* layer = MenuItem::create(); scene->addChild(layer); return scene;}bool MenuItem::init(){ //初始化父类层 CCLayer::init(); //得到窗口的尺寸 CCSize winSize = CCDirector::sharedDirector()->getWinSize(); //创建一个Menu(看不到效果,Menu只是一个容器) CCMenu* menu = CCMenu::create(); //添加Menu addChild(menu); //创建可以来回切换的菜单 //创建猜菜单的状态 CCMenuItem* subItem1 = CCMenuItemFont::create("ON");CCMenuItem* subItem2 = CCMenuItemFont::create("OFF");CCMenuItem* subItem3 = CCMenuItemFont::create("ON/OFF"); CCMenuItemToggle* itemToggle = CCMenuItemToggle::create(subItem1); itemToggle->addSubItem(subItem2); itemToggle->addSubItem(subItem3); //添加菜单项目 menu->addChild(itemToggle);menu->alignItemsVertically(); return true;}
Execution result:
Zookeeper
Zookeeper
Create menu items in Cocos2d-X