0

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

-(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;
    }
}
4

1 回答 1

0

很简单,只要您打开/关闭音量值,只需将它们保存为用户默认值即可。

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

在下次启动时读取用户默认值

BOOL isMusicOn = [[NSUserDefaults standardUserDefaults] boolForKey:@"music"];
于 2013-10-30T10:39:27.267 回答