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

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

概述在上一节中实现了悔棋,在这节将介绍如何在游戏中添加背景音乐 看一下效果: 实现思路: 当单击后游戏中播放背景音乐,并且背景音乐切换按钮由变成 当单击后停止播放背景音乐,并且背景音乐切换按钮由变成 当游戏进入后台后停止播放背景音乐 当游戏恢复后继续播放背景音乐 实现代码: 在SceneGame类中定义一个成员函数Voice(CCObject*)用于实现背景音乐的切换,Voice(CCObject*)

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

看一下效果:


实现思路:

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

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

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

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


实现代码:
在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 toovoID 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 must be pause    SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();}

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

// this function will be called when the app is active againvoID 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 againvoID AppDelegate::applicationWillEnterForeground() {    CCDirector::sharedDirector()->startAnimation();    // if you use SimpleAudioEngine,it must resume here    SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();



 总结

以上是内存溢出为你收集整理的Cocos2d-x开发中国象棋《十一》在游戏中添加背景音乐全部内容,希望文章能够帮你解决Cocos2d-x开发中国象棋《十一》在游戏中添加背景音乐所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1015797.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存