大佬教程收集整理的这篇文章主要介绍了Cocos2d-x 3.x 练习 1 音乐和音效,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
→_→ 一群人讨论做个文字类的AVG。
开始打算用的renpy。在我的建议下还是改用的cocos2d,我觉得这个比较方便,(C++啦,比较成熟啦。。。
然后和以前写过的2048 一比,发现版本差距好大。都跳到3.4 了。
cocos的官网也没找到 版本到底更新了什么。连关键字都不一样了。
CCSprite 改成Sprite。就是CC(cocos)被去掉了。
如此等等……
然后屏幕自适应也不能直接复制以前的。
CCLOG了一下,发现是默认 960x640 。
于是在AppDelegate.cpp 添加了
glview->setFrameSize(854,480); Director::geTinstance()->getOpenGLView()->setDesignResolutionSize(854,480,kResolutionShowAll);
gl的框架设置为854x480。然后按照这个比例进行自适应。
AVG要有BGM啦。
在HelloWorldScene.cpp里测试了一下音乐播发。
#include "SimpleAudioENGIne.h"@H_696_41@CocosDenshion::SimpleAudioENGIne::geTinstance()->playBACkgroundMusic("BACkground.mp3",truE);//true=循环
CocosDenshion::SimpleAudioENGIne::geTinstance()->resumeBACkgroundMusic();
CocosDenshion::SimpleAudioENGIne::geTinstance()->pauseBACkgroundMusic();
CocosDenshion::SimpleAudioENGIne::geTinstance()->preloadBACkgroundMusic("BACkground.mp3");
为了实现 音乐的暂停 和 继续播放。用了一个toggle 菜单。
可以在.h 中 bool 一个 is_pause 变量表示音乐是否暂停。初始设为0。
然后 回调函数就是:
void HelloWorld::pausebgm(Ref* pSender) { if(ispausE) { CocosDenshion::SimpleAudioENGIne::geTinstance()->resumeBACkgroundMusic(); ispause=0; } else { CocosDenshion::SimpleAudioENGIne::geTinstance()->pauseBACkgroundMusic(); ispause=1; } return; }
auto item_on=MenuItemFont::create("On "); item_on->setColor(ccc3(0,255,255)); auto item_off=MenuItemFont::create("Off"); item_off->setColor(ccc3(0,255)); auto pausebgmitem = MenuItemToggle :: createWithCallBACk( CC_CALLBACK_1(HelloWorld::pausebgm,this),//MenuItemFont::create("On "),//MenuItemFont::create("Off"),item_on,item_off,null);
还可以用图片创建
auto item_on=MenuItemImage::create("pausebgm_on.jpg","pausebgm_on.jpg");
auto item_off=MenuItemImage::create("pausebgm_off.jpg","pausebgm_off.jpg");这就就是用图片显示了。别忘了了添加到 toggle 里面,还有toggle的位置和addchild。
音乐相关:
//预加载背景音乐 SimpleAudioENGIne::sharedENGIne()->preloadBACkgroundMusic(music/xxxx.mp3); //开始播放背景音乐,true表示循环 SimpleAudioENGIne::sharedENGIne()->playBACkgroundMusic(music/xxxx.mp3,truE); //停止背景音乐,这是一个缺省参数函数,传参表示是否释放音乐文件 SimpleAudioENGIne::sharedENGIne()->stopBACkgroundMusic(); //暂停背景音乐 SimpleAudioENGIne::sharedENGIne()->pauseBACkgroundMusic(); //重头调用背景音乐 SimpleAudioENGIne::sharedENGIne()->rewindBACkgroundMusic(); //返回布尔型参数,表示是否在放着背景音乐 SimpleAudioENGIne::sharedENGIne()->isBACkgroundMusicPlaying() //设置音量0.0-1.0 SimpleAudioENGIne::sharedENGIne()->setBACkgroundMusicVolume(0.5);
然后就是音效,方法差不多。
音效相关:
//预加载音效 SimpleAudioENGIne::sharedENGIne()->preloadEffect(music/xxxx.mp3); //开始播放音效,false表示不循环 SimpleAudioENGIne::sharedENGIne()->playEffect(music/xxxx.mp3,falsE); //停止音效,可以选择单独停掉一个音效,这个值是由playEffect返回的 SimpleAudioENGIne::sharedENGIne()->stopEffect(SoundId); //停止全部音效 SimpleAudioENGIne::sharedENGIne()->stopAllEffects(); //暂停单个音效 SimpleAudioENGIne::sharedENGIne()->pauseEffect(SoundId); //重新开始音效 SimpleAudioENGIne::sharedENGIne()->resumeEffect(SoundId); //暂停全部音效 SimpleAudioENGIne::sharedENGIne()->pauseAllEffects(); //重新开始全部音效 SimpleAudioENGIne::sharedENGIne()->resumeAllEffects(); //设置音效音量0.0-1.0 SimpleAudioENGIne::sharedENGIne()->setEffectsVolume(0.5); //卸载音效 SimpleAudioENGIne::sharedENGIne()->unloadEffect(music/xxxx.mp3);
SoundId 是 playEffect("xx.mp3")返回的 。
还是预加载音效
CocosDenshion::SimpleAudioENGIne::geTinstance()->preloadEffect("effect.mp3");然后设置一个item
auto test1=MenuItemFont::create("effect",CC_CALLBACK_1(HelloWorld::playe,this)); test1->setPosition(visibleSize/2); test1->setColor(ccc3(255,0));
auto menu = Menu::create(test1,null); menu->setPosition(Vec2::ZERO); this->addChild(menu,1);
回调函数就是播放一次音效。
void HelloWorld::playe(Ref* pSender) { CocosDenshion::SimpleAudioENGIne::geTinstance()->playEffect("effect.mp3",falsE); return; }
这样运行之后 点击这个 effect 就会播发 effect.mp3 。
貌似没法放ogg的。我转换成mp3才成功的。
搞完了新年也就这么来了。无所谓啦。新年还是一样过。
以上是大佬教程为你收集整理的Cocos2d-x 3.x 练习 1 音乐和音效全部内容,希望文章能够帮你解决Cocos2d-x 3.x 练习 1 音乐和音效所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。