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

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

看一下效果:


实现思路:

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

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

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

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


实现代码:
在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();




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值