Game Development Reference
In-Depth Information
Then improve the playCollisionSoundWithPair: method in Listing 11-6 with
the lines highlighted in Listing 11-10 .
Listing 11-10 . Don't play a new sound effect for a short time after a sound effect has star-
ted playing
-(void)
playCollisionSoundWithPair:(CCPhysicsCollisionPair*)pair
{
if (_playingCollisionSound == NO)
{
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];
_playingCollisionSound = YES;
[self scheduleBlock:^(CCTimer *timer) {
_playingCollisionSound = NO;
} delay:0.4];
}
}
Now the sound effect is no longer tied to totalKineticEnergy but plays on every
collision. Without the _playingCollisionSound flag, this will likely cause the
sound to be played repeatedly in a short time frame. Therefore, it is set to YES when play-
ing the sound, and a block is scheduled with a 0.4-second delay to reset the _play-
ingCollisionSound ivar to NO so that the next collision will play another sound ef-
fect.
You should apply this principle to all sounds that must not be played again for a short time
after a sound effect of the same type has begun playing.
Search WWH ::




Custom Search