3 title: Cocos2d-x之CCMenu
4 description: Cocos2dx的菜单
10 <div class="page-header">
11 <h1>一、CCMenuItemSprite <small>精灵按钮</small></h1>
13 <pre class="prettyprint">
14 CCSprite * normal = CCSprite::spriteWithFile("默认状态的图片");
15 CCSprite * selected = CCSprite::spriteWithFile("选中状态的图片");
16 CCSprite * disabled = CCSprite::spriteWithFile("禁用状态的图片");
17 CCMenuItemSprite * item = CCMenuItemSprite::itemFromNormalSprite(normal, selected, disabled, this, menu_selector(Test::callback));
18 // itemFromNormalSprite(normal, selected, disabled);
19 // itemFromNormalSprite(normal, selected, this, menu_selector(Test::callback));
20 // itemFromNormalSprite(normal, selected);
21 item->setIsEnabled(false); // 是否可用,默认true
26 <div class="page-header">
27 <h1>二、CCMenuItemImage <small>图片按钮</small></h1>
29 <pre class="prettyprint">
30 CCMenuItemImage * item = CCMenuItemImage::itemFromNormalImage("默认状态", "选中状态", this, menu_selector(Test::callback));
31 // itemFromNormalImage("默认状态", "选中状态");
32 // itemFromNormalImage("默认", "选中", "禁用", this, menu_selector(Test::callback));
33 // itemFromNormalImage("默认", "选中", "禁用");
38 <div class="page-header">
39 <h1>三、CCMenuItemLabel <small>label按钮</small></h1>
41 <pre class="prettyprint">
42 // 支持CCLabelAtlas、CCLabelBMFont
43 CCLabelAtlas * label = CCLabelAtlas::labelWithString("123", "nums.png", 24, 30, '');
44 CCMenuItemLabel * item = CCMenuItemLabel::itemWithLabel(label, this, menu_selector(Test::callback));
45 // itemWithLabel(label);
46 item->setDisabledColor(ccc3(255, 255, 255));
47 item->setColor(ccc3(0, 0, 0));
52 <div class="page-header">
53 <h1>四、CCMenuItemFont <small>字体按钮</small></h1>
55 <pre class="prettyprint">
56 CCMenuItemFont * item = CCMenuItemFont::itemFromString("文本", this, menu_selector(Test::callback));
57 // itemFromString("文本");
58 item->setFontSizeObj(24);
59 item->setFontName("Monaco");
62 CCMenuItemFont::setFontSize("全局字体大小");
63 CCMenuItemFont::setFontName("全局字体名称");
68 <div class="page-header">
69 <h1>五、CCMenuItemToggle <small>开关式按钮</small></h1>
71 <pre class="prettyprint">
72 CCMenuItemToggle * item = CCMenuItemToggle::itemWithTarget(this,
73 menu_selector(Test::callback),
74 CCMenuItemFont::itemFromString("On"),
75 CCMenuItemFont::itemFromString("Off"),
77 item->getSubItems()->addObject(CCMenuItemFont::itemFromString("Other"));
78 item->setSelectedIndex(2);
83 <div class="page-header">
86 <pre class="prettyprint">
87 CCMenu * menu = CCMenu::menuWithItems(item1, item2, NULL);
88 menu->alignItemsHorizontally(); // 垂直对齐
89 menu->alignItemsVertically(); // 水平对象
90 menu->alignItemsInColumns(2, 1, NULL); // 两行,两列,一列
95 <div class="page-header">
98 <pre class="prettyprint">
99 Test::callback(CCObject * sender)