Cocos2d-x instance: Set background music and sound-AppDelegate implementation, appdelegate

Source: Internet
Author: User
Tags set background

Cocos2d-x instance: Set background music and sound-AppDelegate implementation, appdelegate
To learn more about playing background music and sound effects, let's use an example. As shown in, there are two scenarios: HelloWorld and Setting. In the HelloWorld scenario, click the "game Settings" menu to switch to the Setting scenario. In the Setting scenario, you can set whether to play background music and sound effects, click "OK" to return to the HelloWorld scenario.

 








We need to implement the function of pausing and resuming playing background music in AppDelegate. The Code of AppDelegate. H is as follows:
#ifndef  _APP_DELEGATE_H_#define  _APP_DELEGATE_H_#include "cocos2d.h"#include "SimpleAudioEngine.h"①using namespace CocosDenshion;②class  AppDelegate : private cocos2d::Application{public:    AppDelegate();    virtual ~AppDelegate();    virtual bool applicationDidFinishLaunching();    virtual void applicationDidEnterBackground();    virtual void applicationWillEnterForeground();};#endif // _APP_DELEGATE_H_


The first line of the above Code introduces the header file SimpleAudioEngine. h, which is required by SimpleAudioEngine. The second line of code using namespace CocosDenshion uses the namespace CocosDenshion, which is required by the CocosDenshion engine.
# Include "AppDelegate. h" # include "HelloWorldScene. h" USING_NS_CC; AppDelegate: AppDelegate () {} AppDelegate ::~ AppDelegate () {} bool AppDelegate: applicationDidFinishLaunching () {①... ... // Run director-> runWithScene (scene); // initialize the background music SimpleAudioEngine: getInstance ()-> preloadBackgroundMusic ("sound/jazz#"); ② SimpleAudioEngine: getInstance () -> preloadBackgroundMusic ("sound/synthloud"); ③ // initialize the sound SimpleAudioEngine: getInstance ()-> preloadEffect ("sound/Blip.wav"); ④ return true ;} void AppDelegate: applicationDidEnterBackground () {⑤ ctor: getInstance ()-> stopAnimation (); SimpleAudioEngine: getInstance ()-> pauseBackgroundMusic (); ⑥} void AppDelegate :: applicationWillEnterForeground () {7director: getInstance ()-> startAnimation (); SimpleAudioEngine: getInstance ()-> resumeBackgroundMusic (); role}


In the above Code, row ① declares the applicationDidFinishLaunching () function, which is called at game startup. ② ~ Line 4 of code initializes the background music and audio files.

The fifth line of Code declares that applicationDidEnterBackground () Is the function called when the game enters the day after tomorrow. In this function, you need to stop the animation and pause the background music. The Code in line 7 declares that applicationWillEnterForeground () is called when the game returns to the foreground from the day after tomorrow. In this function, you need to continue playing the animation and background music.


More content please pay attention to the Cocos2d-x series of books "Cocos2d-x practice (Volume I): C ++ development" book exchange discussion site: http://www.c Ocoagame.netWelcome to cocos2d-x Technology Discussion Group: 257760386, 327403678


Cocos2d-x black screen sound loss problems

Check the operations in the AppDelegate: applicationDidEnterBackground and AppDelegate: applicationWillEnterForeground functions. The first is the operation after the program enters the background, and the second is the operation after the program returns to the foreground.

Cocos2d-x cannot play background music

Use preloadBackgroundMusic to load the music and try again. In addition, use mp3 for background music. If not, it is very small that your mp3 format is not standard. You can use the Conversion Tool to re-convert and then try again to solve the problem. Pay attention to the correct position of the music.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.