码迷,mamicode.com
首页 > 其他好文 > 详细

Cocos2d-x开发中国象棋《十一》在游戏中添加背景音乐

时间:2014-11-21 21:53:16      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   for   

上一节中实现了悔棋,在这节将介绍如何在游戏中添加背景音乐

看一下效果:
bubuko.com,布布扣


实现思路:

当单击bubuko.com,布布扣后游戏中播放背景音乐,并且背景音乐切换按钮由bubuko.com,布布扣变成bubuko.com,布布扣

当单击bubuko.com,布布扣后停止播放背景音乐,并且背景音乐切换按钮由bubuko.com,布布扣变成bubuko.com,布布扣

当游戏进入后台后停止播放背景音乐

当游戏恢复后继续播放背景音乐


实现代码:
在SceneGame类中定义一个成员函数Voice(CCObject*)用于实现背景音乐的切换,Voice(CCObject*)中的代码

//播放背景音乐
void SceneGame::Voice(CCObject*)
{
    static int i = 0;

    if(0 == i)
    {
        //播放背景音乐
        CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("floor.wav",true);
        
        //显示停止播放背景音乐按钮
        itemCloseVoice->setVisible(true);

        i++;
    }
    else
    {
        //停止播放背景音乐
        CocosDenshion::SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();

        //隐藏停止播放背景音乐按钮
        itemCloseVoice->setVisible(false);

        i--;
    }
}

将AppDelegate.cpp下的函数void AppDelegate::applicationDidEnterBackground() 中的代码

// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground() {
    CCDirector::sharedDirector()->stopAnimation();

    // if you use SimpleAudioEngine, it must be pause
    //SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}

改成(去掉代码前的注释)

// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground() {
    CCDirector::sharedDirector()->stopAnimation();

    // if you use SimpleAudioEngine, it must be pause
    SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}

将AppDelegate.cpp下的函数void AppDelegate::AppDelegate::applicationWillEnterForeground() 中的代码

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
    CCDirector::sharedDirector()->startAnimation();

    // if you use SimpleAudioEngine, it must resume here
   // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}

改成(去掉代码前的注释)

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
    CCDirector::sharedDirector()->startAnimation();

    // if you use SimpleAudioEngine, it must resume here
    SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();





Cocos2d-x开发中国象棋《十一》在游戏中添加背景音乐

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:http://blog.csdn.net/u010105970/article/details/41358359

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!