From: http://blog.linguofeng.com/archive/2012/11/14/cocos2d-x-CCMenu.html
一、CCMenuItemSprite精灵按钮
CCSprite * normal = CCSprite::spriteWithFile("默认状态的图片"); CCSprite * selected = CCSprite::spriteWithFile("选中状态的图片"); CCSprite * disabled = CCSprite::spriteWithFile("禁用状态的图片"); CCMenuItemSprite * item = CCMenuItemSprite::itemFromNormalSprite(normal, selected, disabled, this, menu_selector(Test::callback)); // itemFromNormalSprite(normal,selected,disabled); // itemFromNormalSprite(normal,this,menu_selector(Test::callback)); // itemFromNormalSprite(normal,selected); item->setIsEnabled(false); // 是否可用,默认true
二、CCMenuItemImage图片按钮
CCMenuItemImage * item = CCMenuItemImage::itemFromNormalImage("默认状态", "选中状态", menu_selector(Test::callback)); // itemFromNormalImage("默认状态","选中状态"); // itemFromNormalImage("默认","选中","禁用",menu_selector(Test::callback)); // itemFromNormalImage("默认","禁用");
三、CCMenuItemLabellabel按钮
// 支持CCLabelAtlas、CCLabelBMFont CCLabelAtlas * label = CCLabelAtlas::labelWithString("123", "nums.png", 24, 30, ''); CCMenuItemLabel * item = CCMenuItemLabel::itemWithLabel(label, menu_selector(Test::callback)); // itemWithLabel(label); item->setDisabledColor(ccc3(255, 255, 255)); item->setColor(ccc3(0, 0, 0));
四、CCMenuItemFont字体按钮
CCMenuItemFont * item = CCMenuItemFont::itemFromString("文本", menu_selector(Test::callback)); // itemFromString("文本"); item->setFontSizeObj(24); item->setFontName("Monaco"); // 全局字体与大小 CCMenuItemFont::setFontSize("全局字体大小"); CCMenuItemFont::setFontName("全局字体名称");
五、CCMenuItemToggle开关式按钮
CCMenuItemToggle * item = CCMenuItemToggle::itemWithTarget(this, menu_selector(Test::callback), CCMenuItemFont::itemFromString("On"), CCMenuItemFont::itemFromString("Off"), NULL); item->getSubItems()->addObject(CCMenuItemFont::itemFromString("Other")); item->setSelectedIndex(2);
六、CCMenu
CCMenu * menu = CCMenu::menuWithItems(item1, item2, NULL); menu->alignItemsHorizontally(); // 垂直对齐 menu->alignItemsVertically(); // 水平对象 menu->alignItemsInColumns(2, 1, NULL); // 两行,两列,一列
七、callback
Test::callback(CCObject * sender) { }