Game Development Reference
In-Depth Information
we'll be getting at next. A
playEffect
volume of 1.0 means the audio file is played at
the same volume as
effectsVolume
.
The
pitch
is randomized between 0.8 and 1.3 to create greater sound variety without
having to use multiple effects. This also shows an example of how to generate floating-
point numbers with
arc4random_uniform
. If you divide a value in the range of 0 to
49 and divide it by 100.0, you get a value in the range of 0.0 to 0.49. Note that it's import-
ant to write
100.0
with the dot or casting the value:
(CGFloat)100
. If the divisor is
simply written as
100
, it is an integer division that always results in
0
.
To quickly test this sound effect playback, add the highlighted line in
Listing 11-7
to the
ccPhysicsCollisionBegin:
method that is called when the player collides with a
spring object.
Listing 11-7
.
Playing a sound effect when colliding with springs
-(BOOL) ccPhysicsCollisionBegin:(CCPhysicsCollisionPair
*)pair
player:(CCNode *)player
spring:(SpringBoard *)spring
{
[spring letGo];
[self playCollisionSoundWithPair:pair];
return YES;
}
You should also add two more
ccPhysicsCollisionBegin
methods, pairing the
player with obstacles and gears. Add the code in
Listing 11-8
below the other
ccPhys-
icsCollisionBegin
methods.
Listing 11-8
.
Playing sound effects when colliding with obstacles and gears
-(BOOL) ccPhysicsCollisionBegin:(CCPhysicsCollisionPair
*)pair
player:(CCNode *)player
obstacle:(CCNode*)obstacle
{
[self playCollisionSoundWithPair:pair];
return YES;
Search WWH ::
Custom Search