Adding looped background music
Cocos2d-x has a separate engine for the sound named CocosDenshion
and uses the SimpleAudioEngine.h
header. Whenever you want to include calls for any of its function, you will have to include this header. Similar to the CCDirector
class, the Simple Audio Engine
is also a singleton class that you will use to start, pause, resume, and stop a particular background sound or effect.
As you would have noticed, there are two types of audio in games—background music, which lasts for about usually 30 seconds to a minute and gets looped over time, and there are sound effects that get played when a certain event occurs, such as shooting a bullet or enemy getting hurt and it lasts a couple of seconds. Both files are called separately using different functions in Cocos2d-x.
Also, the sound effects and background music need to be preloaded earlier to avoid any delay in playback; otherwise, the first time when you play a sound instead of playing the file instantly, the system...