Adding sound effects
Next, we will add the sound effects for the game, but as with the background music, we have to preload the files in advance. So right under where we played the background music in applicationDidFinishLaunching
, add the following code to preload files for six sound effects:
CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect("enemyKill.wav"); CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect("fireRocket.wav"); CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect("gunshot.wav"); CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect("playerKill.wav"); CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect("pop.wav"); CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect("rocketExplode.wav");
Notice that the code looks very similar to how we loaded the background music. The only difference being that instead of preloadBackgroundMusic
, now we have preloadEffect
.
Once the sound effects have...