#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_
#include "AppDelegate.h" #include "HelloWorldScene.h" USING_NS_CC; AppDelegate::AppDelegate() { } AppDelegate::~AppDelegate() { } bool AppDelegate::applicationDidFinishLaunching() { ① … … // run director->runWithScene(scene); //初始化 背景音乐 SimpleAudioEngine::getInstance()->preloadBackgroundMusic("sound/Jazz.mp3"); ② SimpleAudioEngine::getInstance()->preloadBackgroundMusic("sound/Synth.mp3"); ③ //初始化 音效 SimpleAudioEngine::getInstance()->preloadEffect("sound/Blip.wav"); ④ return true; } void AppDelegate::applicationDidEnterBackground() { ⑤ Director::getInstance()->stopAnimation(); SimpleAudioEngine::getInstance()->pauseBackgroundMusic(); ⑥ } void AppDelegate::applicationWillEnterForeground() { ⑦ Director::getInstance()->startAnimation(); SimpleAudioEngine::getInstance()->resumeBackgroundMusic(); ⑧ }
第⑤行代码是声明applicationDidEnterBackground()是游戏进入到后天时候调用函数,在这个函数中需要停止动画和暂停背景音乐播放。第⑦行代码是声明applicationWillEnterForeground()是游戏从后天回到前台时候调用,在这个函数中需要继续动画和背景音乐播放。
Cocos2d-x实例:设置背景音乐与音效- AppDelegate实现,布布扣,bubuko.com
Cocos2d-x实例:设置背景音乐与音效- AppDelegate实现
原文地址:http://blog.csdn.net/tonny_guan/article/details/38389559