我正在尝试使用游戏视图中的SFX和音频按钮连接设置视图中的SFX和音频按钮,但我遇到了一些问题。
问题是
*当用户第一次决定玩游戏时,默认情况下设置视图中的按钮设置应该为ON,但是一旦他选择将其更改为他/她选择设置的内容,就会立即保存...所以下次玩家返回设置时,无论游戏结束后游戏是否由玩家手动结束,终止或进入后台,由于电话等,他们应该仍然是玩家选择拥有的一次他们。
*游戏视图上的按钮设置应与设置视图中的一次匹配,如果玩家选择更改它们,则应保存。因此,下次玩家进入设置视图时,他们应该是他在游戏中选择的一次。
到目前为止我还没有完成这项工作
我有一个处理游戏音频的单例类,然后我有一个实现文件来处理设置以及游戏本身。
这是我为设置视图和游戏视图创建音频设置的方法。
第一部分是设置代码
-(IBAction)options{
saveBackAudioON = [[NSUserDefaults standardUserDefaults] boolForKey:kSaveBackAudioON];
saveSFXAudioON = [[NSUserDefaults standardUserDefaults] boolForKey:kSaveSFXAudioON];
NSLog(@"Options");
OptionsHeadtitle.hidden = NO;
Start.hidden = YES;
option.hidden = YES;
howtoplay.hidden = YES;
filter.hidden = NO;
SFXimage.hidden = NO;
Musicimage.hidden = NO;
credits.hidden = NO;
back.hidden = NO;
newgame.hidden = YES;
continuegame.hidden = YES;
[self SFXswap];
[self MUSICswap];
}
-(IBAction)SFXswap{
BOOL buttonstate = [SingletonSoundManager isSFXOn];
[SingletonSoundManager setSFXOn:!buttonstate];
if(buttonstate){
NSLog(@"SFX Stops");
sfx1.hidden = buttonstate;
sfx2.hidden = !buttonstate;
}
else if(!buttonstate){
NSLog(@"SFX Starts");
sfx1.hidden = buttonstate;
sfx2.hidden = !buttonstate;
}
}
-(IBAction)MUSICswap{
BOOL buttonstate = [SingletonSoundManager isMusicOn];
[SingletonSoundManager setMusicOn:!buttonstate];
if(buttonstate){
NSLog(@"Music Stops");
music1.hidden = buttonstate;
music2.hidden = !buttonstate;
}
else if(!buttonstate){
NSLog(@"Music Starts");
music1.hidden = buttonstate;
music2.hidden =!buttonstate;
}
}
这部分是游戏视图中的设置
-(void)begingame{
Pause.enabled = YES;
musicbutton1.enabled = YES;
musicbutton2.enabled = YES;
sfxbutton1.enabled = YES;
sfxbutton2.enabled = YES;
Clock.hidden = NO;
Pause.hidden = NO;
totalSeconds = 300; //for 5 minutes
//Countdown for ongoing game
countdowntimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(clockcountdown) userInfo:nil repeats:YES];
//If music is on the play background music
BOOL BackgroundMusicbuttonstate = [SingletonSoundManager isMusicOn];
[SingletonSoundManager setMusicOn:!BackgroundMusicbuttonstate];
//show which state the audio is on, if it´s enabled or disabled from the beginning
if(!BackgroundMusicbuttonstate){
musicbutton1.hidden = BackgroundMusicbuttonstate;
musicbutton2.hidden = !BackgroundMusicbuttonstate;
}
//Play background music if the music is enabled from the beginning
//if(musicbutton1.hidden == NO && musicbutton2.hidden == YES){
if(musicbutton1.hidden == NO){
[sharedSoundManager playMusicWithKey:@"Background" timesToRepeat:-1];
newGame = YES;
}
//else if(musicbutton1.hidden == YES && music2.hidden == NO){
else{
[sharedSoundManager pausePlayingMusic];
}
BOOL SFXbuttonstate = [SingletonSoundManager isSFXOn];
[SingletonSoundManager setSFXOn:!SFXbuttonstate];
//show which state the SFX is on, if it´s enabled or disabled from the beginning
if(!SFXbuttonstate){
sfx1.hidden = SFXbuttonstate;
sfx2.hidden = !SFXbuttonstate;
}
//Play SFX if SFX is enabled from the beginning
if(SFXbuttonstate){
newGame = YES;
}
if(sfxbutton2.hidden == NO){
[sharedSoundManager stopPlayingSFX];
}
newGame = NO;
isbrickdissapearing = NO;
rubin1.center = CGPointMake(rubin2.center.x, rubin2.center.y);
[self.view addSubview:rubin1];
}
-(IBAction)SFX{
BOOL buttonstate = [SingletonSoundManager isSFXOn];
[SingletonSoundManager setSFXOn:!buttonstate];
if(buttonstate){
NSLog(@"SFX Stops");
sfxbutton1.hidden = buttonstate;
sfxbutton2.hidden = !buttonstate;
}
else if(!buttonstate){
NSLog(@"SFX Starts");
sfxbutton1.hidden = buttonstate;
sfxbutton2.hidden = !buttonstate;
}
}
-(IBAction)MUSIC{
BOOL buttonstate = [SingletonSoundManager isMusicOn];
[SingletonSoundManager setMusicOn:!buttonstate];
if(buttonstate){
NSLog(@"Music Stops");
musicbutton1.hidden = buttonstate;
musicbutton2.hidden = !buttonstate;
[sharedSoundManager pausePlayingMusic];
}
else if(!buttonstate){
NSLog(@"Music Starts");
musicbutton1.hidden = buttonstate;
musicbutton2.hidden = !buttonstate;
[sharedSoundManager resumePlayingMusic];
}
}
提前感谢!
答案 0 :(得分:0)
请记住,要将某些内容保存到NSUserDefaults,您必须使用以下内容:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:!previousValue forKey:@"soundOn"];
// NSLog(@"mainPadOptions.mainOptions: %@", mainPadOptions.mainOptions);
[prefs synchronize];
确保在希望能够在其他地方使用它们时同步prefs。
每次我更改视图(或更频繁地)时,我都会同步首选项。
然后,当您想要更改声音值/按钮值时,加载布尔值,如下所示:
BOOL soundFXOn = [[[NSUserDefaults standardUserDefaults] objectForKey:@"soundOn"] boolValue];
如果您了解这一点,那么您应该可以访问正确的布尔值。应该是蛋糕从那里弄清楚。