【Cocos2dx】资源目录,播放背景音乐,导入外部库
在Cocos2dx中播放背景音乐是一件非常easy的事情,就一行代码,可是首先要导入Cocos2dx的音频引擎cocosDenshion。
cocosDenshion对cocos2dproject提供了非常方便的声音素材的调用和管理。
同一时候要把要播放的音乐放到project的资源目录才干正确播放
与《【Cocos2dx】Windows平台下Cocos2dx 2.x的下载、安装、配置。打造自己的Helloworld》(点击打开链接)相同通过例如以下的Python命令。创建一个BGMproject之后:
python create_project.py -project BGM -package test.bgm -language cpp
你能够在此project目录中找到Resources目录,你能够发现当中的内容就是早已被我们玩烂的HelloWorld.png与关闭button。
Cocos2dx用到全部图片、音乐资源都在必须在目录中才可以被调用。
我们再拷贝一个Windows自带的实例音乐Kalimba.mp3进去,让我们的Cocos2dx程序在启动时,自己主动循环播放这个Kalimba.mp3音乐,也就是游戏的BGM。
直接打开proj.win32中的HelloCpp.sln。
对HelloCpp或仅仅在HelloWorld这个场景播放BGM,则对其点击属性,例如以下图加入Cocos2dx的音频引擎cocosDenshion所需的包。就是把.(Cocos2dx)\CososDenshion\include这个目录全员引入到此project或此cpp。
点击确定之后。我们仅仅须要在HelloWorldScene.cpp中引入头文件#include "SimpleAudioEngine.h"同一时候在bool HelloWorld::init(){}中增加一行播放背景音乐的代码就能够了。
终于HelloWorldScene.cpp的代码改动例如以下,事实上没改什么。
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"//引入头文件 USING_NS_CC; CCScene* HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create(); // 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create(); // add layer as a child to scene
scene->addChild(layer); // return the scene
return scene;
} // on "init" you need to initialize your instance
bool HelloWorld::init()
{
CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("Kalimba.mp3",true);//播放音乐
//差别于CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("xx.wav");专门用来播放简短的音效
//////////////////////////////
// 1. super init first
if ( !CCLayer::init() )
{
return false;
} CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); /////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it. // add a "close" icon to exit the progress. it's an autorelease object
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback)); pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
origin.y + pCloseItem->getContentSize().height/2)); // create menu, it's an autorelease object
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu, 1); /////////////////////////////
// 3. add your codes below... // add a label shows "Hello World"
// create and initialize a label CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24); // position the label on the center of the screen
pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - pLabel->getContentSize().height)); // add the label as a child to this layer
this->addChild(pLabel, 1); // add "HelloWorld" splash screen"
CCSprite* pSprite = CCSprite::create("HelloWorld.png"); // position the sprite on the center of the screen
pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); // add the sprite as a child to this layer
this->addChild(pSprite, 0); return true;
} void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
#endif
}
执行此程序,就能够听到Kalimba.mp3这个难听得要死的Windows7演示样例音乐了。当中CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("Kalimba.mp3",true);中第一个參数就是此音乐在Resource目录的路径。true代表循环播放。
【Cocos2dx】资源目录,播放背景音乐,导入外部库的更多相关文章
- Cocos2d-x中停止播放背景音乐
停止背景音乐播放代码放置到什么地方比较适合呢?例如:在HelloWorld场景中,主要代码如下: bool HelloWorld::init() { return true; } void Hello ...
- android studio 导入外部库文件,以及将项目中module变成library引用依赖
一:导入如百度地图等的外部类. 步骤:1.首先 将androidstudio项目显示切换到 project 状态显示项目 2.然后添加.jar文件,将所有的.jar文件放入libs文件夹内(libs文 ...
- [Cocos2d-x]解决Android平台ndk-build时不自动删除外部库
参考链接: http://blog.chinaunix.net/uid-26009923-id-3430612.html http://hi.baidu.com/hpyfei/item/52a2b21 ...
- Cocos2d-x中播放背景音乐
背景音乐的播放与停止实例代码如下: SimpleAudioEngine::getInstance()->playBackgroundMusic("sound/Jazz.mp3" ...
- Spring导入外部资源
创建一个数据库连接的 properties jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssmbuil ...
- iOS开发之静态库(三)—— 图片、界面xib等资源文件封装到.a静态库
编译环境:Macbook Air + OS X 10.9.2 + XCode5.1 + iPhone5s(iOS7.0.3) 一.首先将资源文件打包成bundle 新建工程:File -> Ne ...
- 三个特殊资源目录 /res/xml /res/raw 和 /assets
在android开发中,我们离不开资源文件的使用,从drawable到string,再到layout,这些资源都为我们的开发提供了极大的便利,不过我们平时大部分时间接触的资源目录一般都是下面这三个. ...
- 如何导入外部Git仓库到中国源代码托管平台(Git@OSC)
git clone --bare http://git.rcrtm.com/git/dianli.git git clone --mirror https://git.oschina.net/cand ...
- linux下编译ffmpeg 引入外部库x264
Found no assembler Minimum version is nasm-2.13 If you really want to compile without asm, configure ...
随机推荐
- LeetCode.884-两句话中不常见的单词(Uncommon Words from Two Sentences)
这是悦乐书的第338次更新,第362篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第207题(顺位题号是884).我们给出了两个句子A和B.(一个句子是一串空格分隔的单词 ...
- Android开发中的日期格式化
下面的转换符来自Java,但是在android中同样可用.(以下表格内容来自互联网.) 常见日期格式化转换符 转换符 说 明 示 例 %te 一个月中的某一天(1-31) 2 %tb 指定语言环 ...
- SyntaxError: EOL while scanning string literal的解决
2281 python中字符串的最后一个字符是斜杠会导致出错:SyntaxError: EOL while scanning string literal [背景] Python 2.7.2 中想要通 ...
- Leetcode0002--Add Two Numbers 链表求和
[转载请注明]http://www.cnblogs.com/igoslly/p/8672467.html 来看一下题目: You are given two non-empty linked list ...
- spring - quartz - experssion 表达式
字段 允许值 允许的特殊字符 秒 0-59 , - * / 分 0-59 , - * / 小时 0-23 , - * / 日期 1-31 , - * ? / L W C 月份 1-12 或者 JAN- ...
- NetCore下获取项目文件路径
我要获取的是doc/FPFile.xml 百度了一大堆就是找不到解决问题. 把属性更改为始终赋值, XmlDocument xdi = new XmlDocument(); xdi.Load((&qu ...
- 【python】数组去重
直接用set就行,比如: l = [1, 1, 2, 2, 3, 4, 5] s = set(l) c = [i for i in s] print c 结果为: [1, 2, 3, 4, 5] 其中 ...
- js开发性能(一)
随着js技术的发展,性能问题开始被越来越多的人关注,最近了解了一些关于前端性能的问题,这里主要讨论一下在js脚本加载和执行的过程中,我们应该怎么样来提高js的性能. js脚本的处理 初学前端的时候,我 ...
- marquee标签弹幕效果
播放个视频的时候看到很有趣的弹幕,想着前端能不能做个弹幕效果.弹幕是滚动的,所以首先想到了<marquee>标签.但事实上,<marquee>标签不是w3c的标准,只是主流的浏 ...
- jdk?jre?
很多人都搞不懂什么是jdk,什么是jre,只知道电脑安装了这两个就能开发和运行java程序,这里我简单讲讲什么是jdk,什么是jre. jdk,即Java Development Kit,故名思意就是 ...