实现声音开关的代码

 这段代码由 CocoaChina 会员 “wind3215” 分享,非常实用。

//Settings.h
CCMenuItem *soundOnItem;
    CCMenuItem *soundOffItem;
    CCMenuItemToggle *soundToggleItem;
//Settings.m
-(void) soundButtonTapped: (id) sender
{
    if([CDAudioManager sharedManager].mute == TRUE){
        [CDAudioManager sharedManager].mute = FALSE;
    }
    else {
        [CDAudioManager sharedManager].mute = TRUE;
    }
}
-(id)init{
    if((self=[super init])){
        self.isTouchEnabled = YES;
        soundOnItem = [CCMenuItemImage itemFromNormalImage:@"soundon1.gif" selectedImage:@"soundon1.gif" target:nil selector:nil];
        soundOffItem = [CCMenuItemImage itemFromNormalImage:@"soundoff1.png" selectedImage:@"soundoff1.png" target:nil selector:nil];
        soundToggleItem = [CCMenuItemToggle itemWithTarget:self selector:@selector(soundButtonTapped:) items:soundOnItem, soundOffItem, nil];
        CCMenu *bottomMenu = [CCMenu menuWithItems:soundToggleItem, nil];
        bottomMenu.position = ccp(25,95);
        [self addChild: bottomMenu];
    }
    return self;
}

你可能感兴趣的:(IOS开发笔记)