update
[linguofeng.github.com.git] / _posts / 2012-11-14-cocos2d-x-CCMenu.textile
blobce5c601480d8fe945d940d86a4023f48f8d2358a
1 ---
2 layout: post
3 title: Cocos2d-x之CCMenu
4 description: Cocos2dx的菜单
5 categories: [archive]
6 tags: [cocos2d-x]
7 ---
9 <section>
10     <div class="page-header">
11         <h1>一、CCMenuItemSprite <small>精灵按钮</small></h1>
12     </div>
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
22 </pre>
23 </section>
25 <section>
26     <div class="page-header">
27         <h1>二、CCMenuItemImage <small>图片按钮</small></h1>
28     </div>
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("默认", "选中", "禁用");
34 </pre>
35 </section>
37 <section>
38     <div class="page-header">
39         <h1>三、CCMenuItemLabel <small>label按钮</small></h1>
40     </div>
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));
48 </pre>
49 </section>
51 <section>
52     <div class="page-header">
53         <h1>四、CCMenuItemFont <small>字体按钮</small></h1>
54     </div>
55 <pre class="prettyprint">
56 CCMenuItemFont * item = CCMenuItemFont::itemFromString("文本", this, menu_selector(Test::callback));
57                                      // itemFromString("文本");
58 item->setFontSizeObj(24);
59 item->setFontName("Monaco");
61 // 全局字体与大小
62 CCMenuItemFont::setFontSize("全局字体大小");
63 CCMenuItemFont::setFontName("全局字体名称");
64 </pre>
65 </section>
67 <section>
68     <div class="page-header">
69         <h1>五、CCMenuItemToggle <small>开关式按钮</small></h1>
70     </div>
71 <pre class="prettyprint">
72 CCMenuItemToggle * item = CCMenuItemToggle::itemWithTarget(this,
73         menu_selector(Test::callback),
74         CCMenuItemFont::itemFromString("On"),
75         CCMenuItemFont::itemFromString("Off"),
76         NULL);
77 item->getSubItems()->addObject(CCMenuItemFont::itemFromString("Other"));
78 item->setSelectedIndex(2);
79 </pre>
80 </section>
82 <section>
83     <div class="page-header">
84         <h1>六、CCMenu</h1>
85     </div>
86 <pre class="prettyprint">
87 CCMenu * menu = CCMenu::menuWithItems(item1, item2, NULL);
88 menu->alignItemsHorizontally();         // 垂直对齐
89 menu->alignItemsVertically();           // 水平对象
90 menu->alignItemsInColumns(2, 1, NULL);  // 两行,两列,一列
91 </pre>
92 </section>
94 <section>
95     <div class="page-header">
96         <h1>七、callback</h1>
97     </div>
98 <pre class="prettyprint">
99 Test::callback(CCObject * sender)
103 </pre>
104 </section>