如何保存CCMenuItemToggle声音状态

时间:2013-10-30 10:33:38

标签: cocos2d-iphone

我想在使用切换时保存声音状态,这样当我重新开始游戏时它就不会改变。我怎样才能做到这一点?请参阅下面的相关代码。

-(void)volumeItems {

// volume toggle
CCMenuItemImage *vON = [CCMenuItemImage itemWithNormalImage:@"volumeON.png" selectedImage:@"volumeOFF.png"];
CCMenuItemImage *vOFF = [CCMenuItemImage itemWithNormalImage:@"volumeOFF.png" selectedImage:@"volumeON.png"];
vToggle = [CCMenuItemToggle itemWithTarget:self selector:@selector(volumeSel:) items:vON , vOFF , nil];
      .
      .
      .
}

-(void)volumeSel:(id)sender  {

switch (vToggle.selectedIndex) {
    case 0:
        if (![[SettingsManager sharedSettingsManager] isVolON]) {
            [[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"song.mp3"];
            [[[SettingsManager sharedSettingsManager] setIsVolON:YES];
        }
        CCLOG(@"ON");
        break;
    case 1:
        if ([[[SettingsManager sharedSettingsManager] isVolON]) {
            [[SimpleAudioEngine sharedEngine] stopBackgroundMusic];
            [[[SettingsManager sharedSettingsManager] setIsVolON:NO];
        }
        CCLOG(@"oFF");
        break;
    }
}

1 个答案:

答案 0 :(得分:0)

简单,只需在打开/关闭它们时将音量值保存为用户默认值。

BOOL musicOn = [[[SettingsManager sharedSettingsManager] isVolON];
[[NSUserDefaults standardUserDefaults] setBool:musicOn forKey:@"music"];
[[NSUserDefaults standardUserDefaults] synchronize];

在下次发布时读取用户默认值

BOOL isMusicOn = [[NSUserDefaults standardUserDefaults] boolForKey:@"music"];