//设置默认的菜单项字体属性 [CCMenuItemFont setFontSize:30]; [CCMenuItemFont setFontName: @"Courier New"]; //创建一个菜单项 //这个菜单项完全使用精灵完成. //与其说是菜单项,不如说是个按钮,它像按钮一样,提供普通状态,点中状态和禁用状态三种不同状态下需要显示的Sprite //并且,与UIButton类似的,点击事件通过对target的回调方法来实现. CCSprite *spriteNormal = [CCSprite spriteWithFile:@"menu1.png" rect:CGRectMake(0,23*2,115,23)]; CCSprite *spriteSelected = [CCSprite spriteWithFile:@"menu2.png" rect:CGRectMake(0,23*1,115,23)]; CCSprite *spriteDisabled = [CCSprite spriteWithFile:@"menupause.png" rect:CGRectMake(0,23*0,115,23)]; CCMenuItemSprite *item1 = [CCMenuItemSprite itemFromNormalSprite:spriteNormal selectedSprite:spriteSelected disabledSprite:spriteDisabled target:self selector:nil]; //创建第二个菜单项. //这个菜单项与第一个类似,不同之处是直接使用图片完称,比精灵更简洁. CCMenuItem *item2 = [CCMenuItemImage itemFromNormalImage:@"menu1.png" selectedImage:@"menu2.png" target:self selector:nil]; //创建第三个菜单项 //这个菜单项使用的是文本Label方式 //值得一提的是,cocos2d提供了超级方便的自定义字库调用方法. //如下方法中,设置好字库文件,并且以ascii字符排序顺序定义好字库的初始字符,就可以使用字库创建文本Label. //以label方式创建的菜单项,会自动以点击放大作为菜单的操作效果来处理. //如果要求护理特别特殊的点中效果,也可以继承CCMenuItem之后复写selected方法来实现 CCLabelAtlas *labelAtlas = [CCLabelAtlas labelAtlasWithString:@"0123456789" charMapFile:@"fps_images.png" itemWidth:16 itemHeight:24 startCharMap:'.']; CCMenuItemLabel *item3 = [CCMenuItemLabel itemWithLabel:labelAtlas target:self selector:nil]; item3.disabledColor = ccc3(32,32,64); item3.color = ccc3(200,200,255); // 创建第四个菜单项,这是一个纯文本的菜单项. CCMenuItem *item4 = [CCMenuItemFont itemFromString: @"I toggle enable items" target: self selector:nil]; // 创建第五个菜单项. // 这也是一个使用文本配合字库文件创建的菜单项. // 不同的是,这里使用了fnt格式的字库描述文件.关于fnt格式的字库描述文件,我们今后再做详细研究. //CCBitmapFontAtlas *label = [CCBitmapFontAtlas bitmapFontAtlasWithString:@"configuration" fntFile:@"bitmapFontTest3.fnt"]; // CCMenuItemLabel *item5 = [CCMenuItemLabel itemWithLabel:label target:self selector:nil]; // 创建第六个菜单项 // 这是一个最普通的文本菜单项,但是它搭配了一个动画效果.文本颜色,会一直变化 CCMenuItemFont *item6 = [CCMenuItemFont itemFromString: @"Quit" target:self selector:nil]; id color_action = [CCTintBy actionWithDuration:0.5f red:0 green:-255 blue:-255]; id color_back = [color_action reverse]; id seq = [CCSequence actions:color_action, color_back, nil]; [item6 runAction:[CCRepeatForever actionWithAction:seq]]; // 使用上述所有菜单项,构造菜单. CCMenu *menu = [CCMenu menuWithItems: item1, item2, item3, item4, item6, nil]; // 将菜单居中对齐 [menu alignItemsVertically]; // 给菜单制作一个展现效果. // 以下的算法,实现了菜单项交叉从左右飞入的效果,还是很酷滴. // 关于cocos2d各种动画效果的使用,我觉得有必要单独开一篇学习笔记来学习,今天就先略过吧. CGSize s = [[CCDirector sharedDirector] winSize]; int i=0; for( CCNode *child in [menu children] ) { CGPoint dstPoint = child.position; int offset = s.width/2 + 50; if( i % 2 == 0) offset = -offset; child.position = ccp( dstPoint.x + offset, dstPoint.y); [child runAction: [CCEaseElasticOut actionWithAction: [CCMoveBy actionWithDuration:2 position:ccp(dstPoint.x - offset,0)] period: 0.35f] ]; i++; } [item3 retain]; //将第三个菜单项设为禁用,用于演示toggle菜单项的功能 //disabledItem = [item3 retain]; //disabledItem.isEnabled = NO; [self addChild: menu];