cocos2d-iphone 点击菜单项更换菜单项图片,点击其他的还原原图片

看到网上一些人在找 菜单点击菜单项更换图片,使其处于选中状态,点击其他菜单 菜单项 同理,原点中的还原,点的处于选中状态,特小试一下,放出来共享:

方法一:  直接设置item的 selected 和 unselected  当然了,这个方法还是不错的,基本满足大家需求。

方法二:蛋疼一点,自己来设置

init 时 创建菜单及菜单项并设置各菜单项的 selector方法

-(id) init
{
    if( (self=[super init]) )
    {
        CGSize size = [[CCDirector sharedDirector] winSize];
        CCSprite *background = [CCSprite spriteWithFile:@"layerBg.jpg"];
        background.position = ccp(size.width/2, size.height/2);
        
        // add the label as a child to this Layer
        [self addChild: background z:0 tag:9];
        
        flag = 80;    //记录点击 
        
        CCMenuItemSprite *cmLevel = [CCMenuItemSprite itemWithNormalSprite:[CCSprite spriteWithFile:@"cmLevel1.png"] selectedSprite:[CCSprite spriteWithFile:@"cmLevel2.png"] target:self selector:@selector(goLevelLayer:)];
        cmLevel.tag = 80;
        
        CCMenuItemSprite *cmAche = [CCMenuItemSprite itemWithNormalSprite:[CCSprite spriteWithFile:@"cmAche1.png"] selectedSprite:[CCSprite spriteWithFile:@"cmAche2.png"] target:self selector:@selector(goAchievementLayer:)];
        cmAche.tag = 81;
        
        CCMenuItemSprite *cmSet = [CCMenuItemSprite itemWithNormalSprite:[CCSprite spriteWithFile:@"cmSet1.png"] selectedSprite:[CCSprite spriteWithFile:@"cmSet2.png"] target:self selector:@selector(goSettingLayer:)];
        cmSet.tag = 82;
        
        CCMenuItemSprite *cmShop = [CCMenuItemSprite itemWithNormalSprite:[CCSprite spriteWithFile:@"cmShop1.png"] selectedSprite:[CCSprite spriteWithFile:@"cmShop2.png"] target:self selector:@selector(goShopLayer:)];
        cmShop.tag = 83;
        
        CCMenuItemSprite *cmAbout = [CCMenuItemSprite itemWithNormalSprite:[CCSprite spriteWithFile:@"cmAbout1.png"] selectedSprite:[CCSprite spriteWithFile:@"cmAbout2.png"] target:self selector:@selector(goAboutLayer:)];
        cmAbout.tag = 84;
        
        menu = [CCMenu menuWithItems: cmLevel, cmAche, cmSet, cmShop , cmAbout,  nil]; //
        
        [menu alignItemsHorizontally];
        
        [menu alignItemsHorizontallyWithPadding:8.0f];
        
        [menu setPosition:ccp(size.width * 0.5, size.height * 0.9)];
        
        [self addChild:menu z:0 tag:99];
    }
    return self;
}


-(void) goLevelLayer:(id) sender
{
    CCMenuItemSprite *cmItem = (CCMenuItemSprite *)sender;
    if (flag == cmItem.tag)   //被选状态点击无效
    {
        return;
    }
    [self resetMenuItemImg];
    [cmItem setNormalImage:[CCSprite spriteWithFile:@"cmLevel2.png"]];
    flag = cmItem.tag;
}

-(void) goAchievementLayer:(id) sender
{
    CCMenuItemSprite *cmItem = (CCMenuItemSprite *)sender;
    if (flag == cmItem.tag)
    {
        return;
    }
    [self resetMenuItemImg];
    [cmItem setNormalImage:[CCSprite spriteWithFile:@"cmAche2.png"]];
    flag = cmItem.tag;
}

-(void) goSettingLayer:(id) sender
{
    CCMenuItemSprite *cmItem = (CCMenuItemSprite *)sender;
    if (flag == cmItem.tag)
    {
        return;
    }
    [self resetMenuItemImg];
    [cmItem setNormalImage:[CCSprite spriteWithFile:@"cmSet2.png"]];
    flag = cmItem.tag;
}

-(void) goShopLayer:(id) sender
{
    CCMenuItemSprite *cmItem = (CCMenuItemSprite *)sender;
    if (flag == cmItem.tag)
    {
        return;
    }
    [self resetMenuItemImg];
    [cmItem setNormalImage:[CCSprite spriteWithFile:@"cmShop2.png"]];
    flag = cmItem.tag;
}

-(void) goAboutLayer:(id) sender
{
    CCMenuItemSprite *cmItem = (CCMenuItemSprite *)sender;
    if (flag == cmItem.tag)
    {
        return;
    }
    [self resetMenuItemImg];
    [cmItem setNormalImage:[CCSprite spriteWithFile:@"cmAbout2.png"]];
    flag = cmItem.tag;
}


根据 flag (.h 内定义的属性)标记 还原原选中的菜单恢复正常状态


-(void) resetMenuItemImg
{
    if (flag != 0)
    {
        CCMenuItemSprite *cmItem = (CCMenuItemSprite *)[menu getChildByTag:flag];
        switch (flag)
        {
            case 80:
            {
                [cmItem setNormalImage:[CCSprite spriteWithFile:@"cmLevel1.png"]];
                //爱干啥就干啥了,想调哪就调哪,当然了,你如果想无码裸奔也随意了
                break;
            }
            case 81:
            {
                [cmItem setNormalImage:[CCSprite spriteWithFile:@"cmAche1.png"]];
                
                break;
            }
            case 82:
            {
                [cmItem setNormalImage:[CCSprite spriteWithFile:@"cmSet1.png"]];
                
                break;
            }
            case 83:
            {
                [cmItem setNormalImage:[CCSprite spriteWithFile:@"cmShop1.png"]];
                
                break;
            }
            case 84:
            {
                [cmItem setNormalImage:[CCSprite spriteWithFile:@"cmAbout1.png"]];
                
                break;
            }
            default:
                break;
        }
    }
}


OK 大功告成,在此我就不截图了

你可能感兴趣的:(菜单点击更换图片,点击其他菜单恢复图片)