这篇文章中,我们讲下cocos2dx中的菜单,菜单在游戏中非常常见,看下cocos2dx中菜单的结构:
OK,下面我们直接上代码:
CCLabelTTF *menuTTF=CCLabelTTF::create("菜单一", "ArialRoundedMTBold", 50); CCMenuItemLabel *labelItem=CCMenuItemLabel::create(menuTTF, this, menu_selector(HelloWorld::menuItemCallBack)); labelItem->setPosition(ccp(200, 600)); //继承自CCMenuItemLabel CCMenuItemFont *fontItem=CCMenuItemFont::create("菜单二", this, menu_selector(HelloWorld::menuItemCallBack)); fontItem->setPosition(ccp(200, 500)); //继承自CCMenuItemLabel CCMenuItemAtlasFont *atlasFontItem=CCMenuItemAtlasFont::create("33333", "small_blue_number_level-hd.png", 16, 20, '0', this, menu_selector(HelloWorld::menuItemCallBack)); atlasFontItem->setScale(2.0); atlasFontItem->setPosition(ccp(200, 400)); CCSprite *onSprite=CCSprite::create("switchon.png"); CCSprite *offSprite=CCSprite::create("switchoff.png"); //选中未选中状态,还有一个函数里多了一个参数,disabled表示禁用状态 CCMenuItemSprite *spriteItem=CCMenuItemSprite::create(onSprite, offSprite, this, menu_selector(HelloWorld::menuItemCallBack)); spriteItem->setPosition(ccp(200, 300)); //CCMenuItemImage继承自CCMenuItemSprite CCMenuItemImage *imageItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuItemCallBack) ); imageItem->setPosition( ccp(200,200)); //开关,可以作为开关菜单样式 CCMenuItemToggle *toggleItem=CCMenuItemToggle::createWithTarget(this, menu_selector(HelloWorld::menuItemCallBack), CCMenuItemFont::create("无始"),CCMenuItemFont::create("狠人"),NULL); toggleItem->setPosition(ccp(200, 100)); CCMenu* pMenu = CCMenu::create(labelItem,fontItem,atlasFontItem,spriteItem,imageItem ,toggleItem, NULL); pMenu->setPosition( CCPointZero ); this->addChild(pMenu, 1);
OK,就这么简单,我们看下效果: