Game Development Reference
In-Depth Information
would need to increase only the loop's upper bound—even though there's nothing speak-
ing against preloading the three existing audio files without a loop. For example, the fol-
lowing will do the job just as well:
[audio preloadEffect:@"Audio/splat1.caf"];
[audio preloadEffect:@"Audio/splat2.caf"];
[audio preloadEffect:@"Audio/splat3.caf"];
To play back a preloaded sound effect, you would simply play it with the exact same path
you've used to preload it—for instance:
[[OALSimpleAudio sharedInstance] playEffect:@"Audio/
splat3.caf"];
Be sure to watch the Xcode Debug Console ( View Debug Area Activate Console )
for any ObjectAL errors. For instance, if you try to preload or play an audio file that isn't
in the bundle or where you made a mistake in the path, you'll see it logged in the console:
OAL Error: +[OALTools urlForPath:bundle:]: Could not find
full path of file Audio/splash3.caf
When and where to play sound effects is a good design question. In this project, you'll
play the splat sounds whenever the player impacts the gears, springs, chains, and ropes,
but only if the impact was significant. One way to decide that is to look at the CCPhys-
icsCollisionPair object passed into every physics-collision delegate method.
The CCPhysicsCollisionPair method contains two properties, totalImpulse
and totalKineticEnergy , that reveal both the impulse vector (that is, the direction
of impact force) and the total energy dissipated by the impact. The latter may seem odd
for physics buffs: energy doesn't dissipate; it only changes form. However, in computer-
game physics simulations, there is no concept of heat, which is the most common form of
how impact energy is transformed. Therefore, it is simply said that energy dissipates , or is
lost, when there is a forceful impact. Otherwise, the physics simulation couldn't get rid of
the colliding body's velocities, effectively making all collisions “perfect” in the sense that
the sum of the velocities of both impacting bodies would remain the same.
The totalKineticEnergy is only non-zero for significant impacts. We can use that
to simply assume a splat sound needs to be played if the totalKineticEnergy is
non-zero. Add the method in Listing 11-6 to GameScene.m , preferably just below the
ccPhysicsCollisionBegin methods.
Search WWH ::




Custom Search