Game Development Reference
In-Depth Information
Listing 11-6 . Playing impact sounds
-(void)
playCollisionSoundWithPair:(CCPhysicsCollisionPair*)pair
{
if (pair.totalKineticEnergy > 0.0)
{
NSString* splat = [NSString
stringWithFormat:@"Audio/splat%i.caf",
arc4random_uniform(3) + 1];
CGFloat pitch = 0.8 + (arc4random_uniform(50)
/ 100.0);
[[OALSimpleAudio sharedInstance] playEffect:splat
volume:1.0
pitch:pitch
pan:0.0
loop:NO];
}
}
If totalKineticEnergy is greater than zero, the impact dissipated energy and there-
fore must have been significant. In this case, one of three splat sound effects is chosen
randomly. The arc4random_uniform(3) method returns an integer number less than
3—in other words, a number in the range of 0 to 2. By adding 1, you get a number in the
range of 1 to 3.
Tip There are many ways to generate random numbers, but only arc4ran-
dom methods come closest to generating “truly random” numbers. The ar-
c4random_uniform variant specifically generates uniformly distributed
random numbers without (modulo) bias. An explanation would go too far, so
I'll just say it's generally recommended to favor arc4random_uniform()
over rand() and other random-number methods.
The effect is played using OALSimpleAudio 's playEffect variant with the most
parameters. Next to the audio file itself, you can specify a volume to play the effect at.
This volume is not an absolute volume but relative to the effectsVolume property that
Search WWH ::




Custom Search