O'Reilly logo

Cocos2d-x Game Development Essentials by Gareth Jones, Arutosh Gurung, Frahaan Hussain

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Loading and playing background music

This section will cover how to use Cocos2d-x to load and play background music within our game to add an extra dimension to the game.

The following are the main steps to play background music:

  1. Include sound header.
  2. Preload background music file (usually in the init() method).
  3. Play background music, usually at the start of a scene in the init() method.

Adding background music

Let's add background music to the MainMenuScene.cpp file:

  1. Add the sound header to the top of the file (we have already added it for the button-click sound effect):
    #include "SimpleAudioEngine.h"

    The following is how it will look in the MainMenuScene.cpp file:

  2. Check whether the background music is already playing. If it isn't, preload the background ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required