cocos2d-x提供了多种菜单项,分别为:CCMenuItemLable、CCMenuItemSprite、CCMenuItemToggle、CCMenuItemAtlasFont、CCMenuItemFont及CCMenuItemImage他们都继承CCMenuItem,继承图如下:
CCMenuItemLable:
主要包括CCMenuItemFont和CCMenuItemAtlasFont。
CCMenuItemFont:
[cpp]
CCMenuItemFont::setFontName( "Marker Felt" );
CCMenuItemFont::setFontSize(34);
CCMenuItemFont *pCloseItemFont = CCMenuItemFont::create("CloseFont",
this,menu_selector(HelloWorld::menuCloseCallback));
pCloseItemFont->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width-100,20));
"CloseFont"是菜单显示的文本
menu_selector(HelloWorld::menuCloseCallback)是一个回调函数,点击菜单后会调用menuCloseCallback()函数
setFontName 设定显示文本的字体
setFontSize 设定显示文本的字体大小
CCMenuItemAtlasFont:
[cpp]
CCLabelAtlas *pLabelAtlas = CCLabelAtlas::create("0123456789","labelatlas.png",16,24,'.'); //上一篇的文本中已经讲过了
CCMenuItemLabel *pAtlasFont = CCMenuItemLabel::create(pLabelAtlas,
this,menu_selector(HelloWorld::menuCloseCallback));
pAtlasFont->setPosition(
ccp(CCDirector::sharedDirector()->getWinSize().width/2,200));
pAtlasFont->setDisabledColor( ccc3(32,32,64) ); //禁用时的颜色
pAtlasFont->setColor( ccc3(200,200,255) ); //显示颜色
CCMenuItemAtlasFont则是通过png文件生成菜单项,即配置文件所对应的图片
还可以使用CCLabelBMFont:
[cpp]
CCLabelBMFont *pLabelBMFont = CCLabelBMFont::create("HelloWorld","dmTest.fnt");
CCMenuItemLabel *pBMFont = CCMenuItemLabel::create(pLabelBMFont,
this,menu_selector(HelloWorld::menuCloseCallback));
pBMFont->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width/2,160));
pBMFont->setScale(0.8f);
因为不能设定字体的大小,所以用setScale函数进行缩放
CCMenuItemSprite:
图片菜单:包括CCMenuItemImage,可以封装图片进入菜单项:
[cpp]
CCSprite *pSt1 = CCSprite::create("Normal.png"); //CCSprite为精灵类
CC_BREAK_IF(!pSt1);
CCSprite *pSt2 = CCSprite::create("Selected.png");
CC_BREAK_IF(!pSt2);
CCSprite *pSt3 = CCSprite::create("Disabled.png");
CC_BREAK_IF(!pSt3)
CCMenuItemSprite *pSprites = CCMenuItemSprite::create(pSt1,pSt2,
pSt3,this,menu_selector(HelloWorld::menuCloseCallback));
pSprites->setPosition(
ccp(CCDirector::sharedDirector()->getWinSize().width-20,60));
CCMenuItemImage:
[cpp]
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback));
CC_BREAK_IF(! pCloseItem);
pCloseItem->setPosition(
CCDirector::sharedDirector()->getWinSize().width - 20, 20));
CCMenuItemToggle:
可以将任意的CCMenuItem封装进去,作为一个开关式的按钮,它的参数是可变的,也就是说可以一下子封装多个
[cpp]
//CCMenuItemToggle
CCMenuItemToggle *pToggle = CCMenuItemToggle::createWithTarget(this,
menu_selector(HelloWorld::menuCloseCallback),
CCMenuItemImage::create("testToggle.png","testToggle2.png"),
CCMenuItemImage::create(
"testToggle2.png","testToggle.png"),NULL);
pToggle->setPosition(
ccp(CCDirector::sharedDirector()->getWinSize().width-350,20));
最后要创建个菜单将所有菜单项添加进去
[cpp]
// Create a menu with the "close" menu item, it's an auto release object.
CCMenu* pMenu = CCMenu::create(pCloseItem,pCloseItemFont,pCloseItemLabel,
pToggle,pSprites,pAtlasFont, NULL);
pMenu->setPosition(CCPointZero);
CC_BREAK_IF(! pMenu);
// Add the menu to HelloWorld layer as a child layer.
this->addChild(pMenu, 1);
[cpp]
void HelloWorld::menuCloseCallback(CCObject* pSender) //菜单项回调函数
{
// "close" menu item clicked
CCDirector::sharedDirector()->end();
}