CCMenu是一个菜单项的容器,用来装载各种菜单项, 菜单项可以是图片、系统字、或者自定义字体。
类图:
菜单项常用设置:
void alignItemsVertically(); //垂直方向默认间隙排列, 5个像素 void alignItemsVerticallyWithPadding(float padding);//垂直方向以padding间隙排列 void alignItemsHorizontally();//水平方向默认间隙排列 void alignItemsHorizontallyWithPadding(float padding);//水平方向以padding间隙排列, 5个像素 void alignItemsInColumns(unsigned int columns, ...);//将items按照列排列 void alignItemsInColumns(unsigned int columns, va_list args); void alignItemsInRows(unsigned int rows, ...);//将items按照行排列 void alignItemsInRows(unsigned int rows, va_list args);
CCMenuItemImage菜单图1:
程序代码:
bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); CCLOG("origin.x: %f, origin.y: %f", origin.x, origin.y) ; ///////////////////////////// // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. // add a "close" icon to exit the progress. it's an autorelease object CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback)); pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 , origin.y + pCloseItem->getContentSize().height/2)); // create menu, it's an autorelease object CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu, 1); ///////////////////////////// // add your codes below... //创建2菜单,每个菜单包括3个菜单项 for (int i=0; i<2; i++) { //创建菜单项 CCMenuItemImage *item1 = CCMenuItemImage::create("btn-about-normal.png", "btn-about-selected.png", this, menu_selector(HelloWorld::menuCallback)); CCMenuItemImage *item2 = CCMenuItemImage::create("btn-highscores-normal.png", "btn-highscores-selected.png", this, menu_selector(HelloWorld::menuCallback2)); CCMenuItemImage *item3 = CCMenuItemImage::create("btn-play-normal.png", "btn-play-selected.png", this, menu_selector(HelloWorld::menuCallback3)); //缩放 item1->setScaleX( 1.5f ); item2->setScaleX( 0.5f ); item3->setScaleX( 0.5f ); //创建菜单,设置位置 CCMenu* menu = CCMenu::create(item1, item2, item3, NULL); CCSize s = CCDirector::sharedDirector()->getWinSize(); menu->setPosition(ccp(s.width/2, s.height/2)); menu->setTag( kTagMenu ); this->addChild(menu, 0, 100+i);//每个菜单包含标签 m_centeredMenu = menu->getPosition(); //保存菜单坐标 } m_alignedH = true; //水平对齐标记 alignMenusH();//菜单水平对齐 return true; } void HelloWorld::menuCallback(CCObject* pSender) { } //第二个菜单项事件处理 void HelloWorld::menuCallback2(CCObject* pSender) { CCMenu *menu = (CCMenu*)(((CCNode*)(pSender))->getParent());//获取菜单指针 GLubyte opacity = menu->getOpacity();//获取菜单透明度 if (opacity==80)//若为100,设置不透明,否则设置透明度100 { menu->setOpacity(255); } else menu->setOpacity(80); } //第三个菜单项事件处理 void HelloWorld::menuCallback3(CCObject* pSender) { m_alignedH = ! m_alignedH; if( m_alignedH ) //刚开始是水平对齐 alignMenusH(); else alignMenusV(); } //水平对齐 void HelloWorld::alignMenusH() { for(int i=0;i<2;i++) { CCMenu *menu = (CCMenu*)getChildByTag(100+i);//根据标记获取菜单 menu->setPosition( m_centeredMenu );//初始时菜单的位置 if(i==0) //第1个菜单位置 { //水平方向5个像素对齐 menu->alignItemsHorizontally(); CCPoint p = menu->getPosition(); menu->setPosition( ccpAdd(p, ccp(0,30)) );//p.x+30, p.y+30 } else //第2个菜单位置 { ////水平方向40个像素对齐 menu->alignItemsHorizontallyWithPadding(40); CCPoint p = menu->getPosition(); menu->setPosition( ccpSub(p, ccp(0,30)) );//p.x-30, p.y-30 } } } //垂直对齐 void HelloWorld::alignMenusV() { for(int i=0;i<2;i++) { CCMenu *menu = (CCMenu*)getChildByTag(100+i); menu->setPosition( m_centeredMenu ); if(i==0) { //垂直方向5个像素对齐 menu->alignItemsVertically(); CCPoint p = menu->getPosition(); menu->setPosition( ccpAdd(p, ccp(100,0)) ); } else { //垂直方向40个像素对齐 menu->alignItemsVerticallyWithPadding(40); CCPoint p = menu->getPosition(); menu->setPosition( ccpSub(p, ccp(100,0)) ); } } }