Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What do you use to play sounds? I think you could disable sounds by deactivating the audio session – see <a href="http://developer.apple.com/IPhone/library/documentation/AudioToolbox/Reference/AudioSessionServicesReference/Reference/reference.html#//apple_ref/c/func/AudioSessionSetActive" rel="nofollow noreferrer">AudioSessionSetActive</a>.</p> <hr> <p><em>Update:</em> 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:</p> <pre><code>@implementation Model - (void) stepBy: (double) seconds { [player doSomething]; if (player.isDead) [self sendNotification:@selector(playerHasDied:) withObject:player]; } @end </code></pre> <p>And the sound view:</p> <pre><code>@implementation SFX - (void) playerHasDied: (id) player { [aarghSound play]; } @end </code></pre> <p>Of course you have to implement the actual observing part. You could use <code>NSNotificationCenter</code> or write your own dispatching code using an array of observers:</p> <pre><code>@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 </code></pre> <p>The SFX view is connected to the model:</p> <pre><code>Model *model = [[Model alloc] init]; SFX *sounds = [[SFX alloc] init]; [model addObserver:sounds]; </code></pre> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload