标签:cocos2dx
菜单项CCMenuItem是一个基类, 子类CCMenuItemFont、CCMenuItemLabel、CCMenuItemSprite、CCMenuItemToggle可加入CCMenu中形成菜单。
程序代码:
bool HelloWorld::init() { if ( !CCLayer::init() ) { return false; } CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); 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 code... //使用字体定义菜单项,包括 CCMenuItemAtlasFont, CCMenuItemFont 两种方式,CCMenuItemFont通过设置字体名称来设置字体。 CCMenuItemFont::setFontSize(30); //字号 CCMenuItemFont::setFontName("Courier New");//字体 CCMenuItemFont *item = CCMenuItemFont::create("--- Go Back ---", this, menu_selector(HelloWorld::menuItemFontcall));//参数1:文字内容,参数2:执行回调函数的目标类,参数3:按下触发的回调函数 //item->setPosition(ccp(visibleSize.width/2,100)); //通过字体配置PNG文件的 CCLabelAtlas CCLabelAtlas *atlasfont = CCLabelAtlas::create("0123456789", "labelatlas.png", 16, 24, ‘.‘);//参数顺序:文字内容,图片,单个文字宽,单个文字高,默认显示字符 CCMenuItemLabel *item1 = CCMenuItemLabel::create(atlasfont, this, menu_selector(HelloWorld::menuItemFontcall2));//参数顺序:文字渲染类,执行回调函数的目标类,按下触发的回调函数 //item1->setPosition(ccp(visibleSize.width/2,400)); //精灵菜单项 CCSprite *spriteNormal = CCSprite::create("menuitemsprite.png", CCRectMake(0,23*2,115,23)); CCSprite *spriteSelected = CCSprite::create("menuitemsprite.png", CCRectMake(0,23*1,115,23)); CCSprite *spriteDisabled = CCSprite::create("menuitemsprite.png", CCRectMake(0,23*0,115,23)); CCMenuItemSprite* item3 = CCMenuItemSprite::create(spriteNormal, spriteSelected, spriteDisabled, this, menu_selector(HelloWorld::menuCallback3));//参数:3种菜单状态及回调函数 //item3->setPosition(ccp(visibleSize.width/2,300)); //通过字体配置FNT类型文件的CCLabelBMFont CCLabelBMFont* label = CCLabelBMFont::create("Enable AtlasItem", "bitmapFontTest3.fnt"); CCMenuItemLabel* item4 = CCMenuItemLabel::create(label, this, menu_selector(HelloWorld::menuCallback4) ); //item4->setPosition(ccp(visibleSize.width/2+50,250)); //触发器菜单项,可以将任何菜单项传递进去 CCMenuItemToggle *toggle = CCMenuItemToggle::createWithTarget( this, menu_selector(HelloWorld::menuCallback5), CCMenuItemFont::create("On"), CCMenuItemFont::create("Off"), NULL); //toggle->setPosition(ccp(visibleSize.width/2+100, 150)); CCMenu *menu = CCMenu::create(item, item1, item3, item4, toggle, NULL); menu->alignItemsVertically(); CCSize s = CCDirector::sharedDirector()->getWinSize(); int i=0; CCNode* child; CCArray * pArray = menu->getChildren(); //获取menu所有节点(菜单项) 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++; } menu->setPosition(ccp(s.width/2, s.height/2)); this->addChild(menu); return true; } void HelloWorld::menuItemFontcall(CCObject* pSender) { CCLOG("menuItemFontcall..."); } void HelloWorld::menuItemFontcall2(CCObject* pSender) { CCLOG("menuItemFontcall2..."); } void HelloWorld::menuCallback3(CCObject* pSender) { CCLOG("menuCallback3..."); } void HelloWorld::menuCallback4(CCObject* pSender) { CCLOG("menuCallback4..."); } void HelloWorld::menuCallback5(CCObject* pSender) { CCLOG("menuCallback5..."); }
【Cocosd2d-x CCMenu菜单之二】,布布扣,bubuko.com
标签:cocos2dx
原文地址:http://blog.csdn.net/oyangyufu/article/details/25972029