转载请说明出处:http://blog.csdn.net/lsmfeixiang/article/details/42223875
github地址:https://github.com/teffy/cocos2dx
接着看教程:播放音乐与音效,然后想实现一个播放,暂停,停止的小功能,就附加看了一下Menu的一些知识,又回顾了一下坐标系
音乐和音效的播放主要使用的SimpleAudioEngine,目前看3.x里面SimpleAudioEngine的注释没有说depart,所以应该和2.x是一直的。
1、先导入头文件和命名空间
#include <SimpleAudioEngine.h>
using namespace CocosDenshion;
2、SimpleAudioEngine::getInstance()初始化AudioEngine,在Scene的init中调用
SimpleAudioEngine::getInstance()->preloadBackgroundMusic(BG_MUSIC);
SimpleAudioEngine::getInstance()->preloadEffect(DOG_WANGWANG);
预加载音频文件
3、然后在相应的事件处理中,就可以调用相关方法,比如:
SimpleAudioEngine::getInstance()->playBackgroundMusic(BG_MUSIC,true);
dog_wangwang = SimpleAudioEngine::getInstance()->playEffect(DOG_WANGWANG);
SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
SimpleAudioEngine::getInstance()->resumeEffect(dog_wangwang);
if (SimpleAudioEngine::getInstance()->isBackgroundMusicPlaying()){
SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
}
if (dog_wangwang > 0){
SimpleAudioEngine::getInstance()->pauseEffect(dog_wangwang);
}
SimpleAudioEngine::getInstance()->stopBackgroundMusic(true);
SimpleAudioEngine::getInstance()->stopAllEffects();
还有一些方法,可以查看定义。
Menu继承关系如图:
简单使用
auto playBgMusic = MenuItemFont::create("Paly BgMusic", CC_CALLBACK_1(HelloWorld::menuPlayBgMusicCallback, this));
playBgMusic->setPosition(Vec2(visibleSize.width / 5, visibleSize.height / 6));
// create menu, it's an autorelease object
auto menu = Menu::create(closeItem, playBgMusic, playEffect, pause,resume,stop, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1);
void HelloWorld::menuPlayBgMusicCallback(Ref* pSender){
SimpleAudioEngine::getInstance()->playBackgroundMusic(BG_MUSIC,true);
}
更多的可以去下面的资源地址里看一下
相关资源地址:
教程:http://blog.csdn.net/u012945598/article/details/16992389
Menu:http://www.tuicool.com/articles/immUVn http://my.oschina.net/Jacedy/blog/301709
坐标系:http://cn.cocos2d-x.org/article/index?type=cocos2d-x&url=/doc/cocos-docs-master/manual/framework/native/v3/coordinate-system/zh.md
demo地址:http://download.csdn.net/detail/lsmfeixiang/8309609