What do you use to play sounds? I think you could disable sounds by deactivating the audio session – see AudioSessionSetActive.
Update: Yes, you’re right. I’ve just tried deactivating the audio session and the sounds seemed to go on. Nevermind. You could use the boolean flag approach, and there is no need to have a condition around every sound. The way I do SFX in my game is through a separate class, a kind of ‘view’ that observes the model and creates sounds for various game events. This way you keep a clean separation of concerns in the design and when you want to switch the sounds off, you simply disconnect the sound class from the model. The code looks a bit like this:
@implementation Model
- (void) stepBy: (double) seconds
{
[player doSomething];
if (player.isDead)
[self sendNotification:@selector(playerHasDied:) withObject:player];
}
@end
And the sound view:
@implementation SFX
- (void) playerHasDied: (id) player
{
[aarghSound play];
}
@end
Of course you have to implement the actual observing part. You could use NSNotificationCenter
or write your own dispatching code using an array of observers:
@implementation Model
- (void) addObserver: (id) object
{
[observers addObject:object];
}
- (void) sendNotification: (SEL) message
{
for (id observer in observers)
if ([observer respondsToSelector:message])
[observer performSelector:message];
}
@end
The SFX view is connected to the model:
Model *model = [[Model alloc] init];
SFX *sounds = [[SFX alloc] init];
[model addObserver:sounds];
When you want to disable all sounds, you just disconnect the SFX from the model; stop observing. If the sounds are disabled before the game starts, you even do not have to allocate the SFX class – this saves time, performance and memory.