首先建菜单类作为容器,把新建菜单项放入菜单容器中,而后将菜单类对象作为一个节点加入布景层。
MenuLayerMainMenu::MenuLayerMainMenu()
{
setTouchEnabled(true); //开启触摸响应
setTouchPriority(kCCMenuHandlerPriority + 1); /*设置触摸事件优先级,注:在Cocos2d上事件的响应并不会按照addChild(object)的添加顺序去响应会个精灵或层的事件的,而是按照优先级priority的大小去分发事件,另外priority值越小优先级越高,在cocos2d默认下 CCMenu的优先级的最高的kCCMenuHandlerPriority= -128;*/
setTouchMode(kCCTouchesOneByOne); //设置触摸模式
// Font Item
/*CCRectMake(0,23*n,115,23)截取图片以点cpp(0,23*n)为左上角截取边框大小(width,height)为(115,,23)的矩形图片,在ios上坐标系从左上角为原点,x轴向右,y轴向下。*/
CCSprite*spriteNormal = CCSprite::create(s_MenuItem, CCRectMake(0,23*2,115,23));
CCSprite*spriteSelected = CCSprite::create(s_MenuItem, CCRectMake(0,23*1,115,23));
CCSprite*spriteDisabled = CCSprite::create(s_MenuItem, CCRectMake(0,23*0,115,23));
/*CCMenuItemSprite::create前三个参数为普通状态、选中状态、无效状态下的按钮,后两个参数为回调函数目标和将回调函数的名称传入选择器中。*/
CCMenuItemSprite*item1 = CCMenuItemSprite::create(spriteNormal, spriteSelected,
spriteDisabled,this,menu_selector(MenuLayerMainMenu::menuCallback) );
// Image Item//CCMenuItemImage是CCMenuItemSprite的子类,与CCMenuItemSprite的区别是子类直接以图片的路径定义
CCMenuItem*item2 = CCMenuItemImage::create(s_SendScore, s_PressSendScore, this,
menu_selector(MenuLayerMainMenu::menuCallback2));// Label Item (LabelAtlas)
//CCLabelAtlas类以图片作为文字,通过图片直接定义,参数分别为显示的字符串、图片路径、字符宽度、字符高度、起始字符
CCLabelAtlas*labelAtlas = CCLabelAtlas::create("0123456789","fonts/labelatlas.png", 16, 24, '.');
CCMenuItemLabel*item3 = CCMenuItemLabel::create(labelAtlas, this,
menu_selector(MenuLayerMainMenu::menuCallbackDisabled));
item3->setDisabledColor(ccc3(32,32,64) );//设置不能点击的颜色(灰色)
item3->setColor( ccc3(200,200,255) );//设置标签颜色
// Font Item
//CCMenuItemFont通过设定系统自带的字体名称设置字体
CCMenuItemFont*item4 = CCMenuItemFont::create("I toggle enable items", this,
menu_selector(MenuLayerMainMenu::menuCallbackEnable));
item4->setFontSizeObj(20);//设置字号
item4->setFontName("MarkerFelt"); //设置系统字体
// Label Item (CCLabelBMFont)
//CCLabelBMFont文字渲染标签类(每个字都是一个精灵)
CCLabelBMFont*label = CCLabelBMFont::create("configuration",
"fonts/bitmapFontTest3.fnt");
CCMenuItemLabel*item5 = CCMenuItemLabel::create(label, this,
menu_selector(MenuLayerMainMenu::menuCallbackConfig));
// Testing issue #500
item5->setScale(0.8f );//设置显示比例
// Events
CCMenuItemFont::setFontName("MarkerFelt");
CCMenuItemFont*item6 = CCMenuItemFont::create("Priority Test", this,
menu_selector(MenuLayerMainMenu::menuCallbackPriorityTest));
// Bugs Item
CCMenuItemFont*item7 = CCMenuItemFont::create("Bugs", this,
menu_selector(MenuLayerMainMenu::menuCallbackBugsTest));
// Font Item
CCMenuItemFont*item8 = CCMenuItemFont::create("Quit", this,
menu_selector(MenuLayerMainMenu::onQuit));
CCMenuItemFont* item9 =CCMenuItemFont::create( "Remove menu item when moving",
this,
menu_selector(MenuLayerMainMenu::menuMovingCallback));
CCActionInterval* color_action =CCTintBy::create(0.5f, 0, -255, -255);
CCActionInterval*color_back = color_action->reverse();
CCSequence* seq =CCSequence::create(color_action, color_back, NULL);
item8->runAction(CCRepeatForever::create(seq));
//menu容器
CCMenu* menu =CCMenu::create( item1, item2, item3, item4, item5,
item6, item7, item8, item9, NULL);
menu->alignItemsVertically();//让子Item按垂直方向自动排队
// elastic effect
CCSize s =CCDirector::sharedDirector()->getWinSize();
int i=0;
CCNode* child;
CCArray * pArray= menu->getChildren();//获取所有子节点
CCObject*pObject = NULL;
CCARRAY_FOREACH(pArray,pObject)//遍历数组
{
if(pObject == NULL) break;
child = (CCNode*)pObject;
CCPoint dstPoint = child->getPosition();
int offset = (int) (s.width/2 + 50);
if( i % 2 == 0)offset = -offset; child->setPosition( ccp( dstPoint.x + offset, dstPoint.y));
//执行并为其添加弹性缓冲移动,动态效果
child->runAction(CCEaseElasticOut::create(CCMoveBy::create(2,ccp(dstPoint.x- offset,0)), 0.35f) );
i++;
}
m_disabledItem =item3;
item3->retain();
m_disabledItem->setEnabled(false );//设置第三个菜单项不可用。
addChild(menu);menu->setPosition(ccp(s.width/2, s.height/2));
}
通过菜单项定义菜单,然后处理菜单项的初始位置,为它们加入出场缓冲动作,并将第三个菜单项设为不可用。