Modifying the sound effect on the fly

One thing that's so cool about using OALSimpleAudio (and was also true about SimpleAudioEngine) is that you can modify how the audio file sounds when it gets played back to the user. For example, if you wish to have a series of coins collected, and each coin collected in rapid succession plays a slightly higher-pitched sound than the previous one, you can simply modify the pitch based on how many coins were collected.

The volume (or gain), pitch, and pan

With one simple call that adds a few parameters to the default playEffect method, you can modify the loudness of the effect, the pitch of the sound effect, and where in the speakers your effect plays. You can do so with the following code:

//volume range: 0.0 to 1.0
//pitch range: 0.0 to inf (1.0 is normal)
//pan range:  -1.0 to 1.0 (far left to far right)
//loop:  If YES, will play until stop is called on the sound
[[OALSimpleAudio sharedInstance] playEffect:@"soundEffect.mp3" volume:1 pitch:1 pan:1 loop:NO];

Stopping looped sound effects

If you've said "Yes" to the preceding loop and wish to stop it at some point, you have to grab the return value of the preceding function, like this:

id<ALSoundSource> effect;

effect = [[OALSimpleAudio sharedInstance] playEffect:@"soundEffect.mp3" volume:1 pitch:1 pan:1 loop:NO];

Then call the respective stop function on the variable:

[effect stop];

Modifying the combine sound effect

Ideally, we don't want the user to hear the same sound effect over and over every time they combine units. Not only will it become obnoxious and annoying to the user but it will also make the game feel more boring and less exciting. With that said, we want to modify the combining sound effect so slightly so that as the user gets a higher number of unit combinations, they feel emboldened by their success, making them wish to play longer.

One of the approaches is to modify the pitch of the sound effect. This will work up to a certain point, until the sound effect becomes so pushed in one direction that it's simply better to provide another sound effect for truly large unit combinations.

Open the MainScene.m file and scroll to the playUnitCombineSoundWithValue method. Here, you're going to modify the code to look something like this:

CGFloat pitchValue = 1.0 - (total / 100.f);
//eg: fv+ov = 20 ... 1.0 - 0.2 = 0.8
if (total < 50)
{
  [[OALSimpleAudio sharedInstance] playEffect:@"unitCombine.mp3"
    volume:1 pitch:pitchValue pan:0 loop:NO];
}

else
{
  [[OALSimpleAudio sharedInstance]
    playEffect:@"largeUnitCombine.mp3"];
}

When you run the game and combine a unit, you will hear the sound effect getting deeper and deeper until a certain value (the tipping point—in this case, a new unit value of 50 or more). At this point, we want to play a different sound effect, which is what the inner if statement handles.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.88.54