《Cocos2d-x游戏开发实战精解》学习笔记3--在Cocos2d-x中播放声音

   之前的内容主要都是介绍如何在屏幕上显示图像,事实上除了图像之外,音乐的播放也可以被理解为一种显示的方式,本节将学习在Cocos2d-x中播放声音的方法。

1)在HelloWorld.h中对HelloWorld类进行如下定义:

class HelloWorld : public Cocos2d::Layer
{
public:
       bool is_paused;
    static Cocos2d::Scene* createScene();
    virtual bool init();
       void play(Cocos2d::Object* pSender);         //播放音乐
       void stop(Cocos2d::Object* pSender);         //停止音乐
       void pause(Cocos2d::Object* pSender);              //暂停
    CREATE_FUNC(HelloWorld);
};

2)在HelloWorldScene.cpp中实现这些方法,如范例3-7所示,完整代码可见源文件本章目录下的项目ChapterThree05

【范例3-7 Cocos2d-x中实现音乐的播放和暂停等操作】

#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
USING_NS_CC;
Scene* HelloWorld::createScene()
{
    auto scene = Scene::create();
    auto layer = HelloWorld::create();
    scene->addChild(layer);
    return scene;
}
bool HelloWorld::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
       is_paused = false;
       //播放按钮
       auto* label_play = Label::create("play", "Arial", 40);
       auto* pLabel_play = MenuItemLabel::create(label_play, this, menu_selector(HelloWorld::play));
       auto* button_play = Menu::create(pLabel_play, NULL);
       button_play->setPosition(160,180);
       addChild(button_play);
       //暂停按钮
       auto* label_pause = Label::create("pause", "Arial", 40);
       auto* pLabel_pause = MenuItemLabel::create(label_pause, this, menu_selector(HelloWorld::pause
       auto* button_pause = Menu::create(pLabel_pause, NULL);
       button_pause->setPosition(320,180);
       addChild(button_pause);30
       //停止按钮
       auto* label_stop = Label::create("stop", "Arial", 40);
       auto* pLabel_stop = MenuItemLabel::create(label_stop, this, menu_selector(HelloWorld::stop));
       auto* button_stop = Menu::create(pLabel_stop, NULL);
       button_stop->setPosition(480,180);
       addChild(button_stop);
       return true;
}
void HelloWorld::play(Cocos2d::Object* pSender)
{     //如果背景音乐被暂停则继续播放
       if (is_paused)
       {
              CocosDenshion::SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
       }
       else
       {     //否则重新播放
              CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("music.mp3");
       }
       is_paused = false;
}
void HelloWorld::stop(Cocos2d::Object* pSender)
{     //停止音乐
       is_paused = false;
       CocosDenshion::SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
}
void HelloWorld::pause(Cocos2d::Object* pSender)
{     //暂停播放音乐
       is_paused = true;
       CocosDenshion::SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}

本例运行后的界面如图3-12所示,点击屏幕上的3个标签按钮则会执行音乐的播放、暂停等操作。

210939_JlNe_2633993.png

3-12 可以点击按钮进行音乐的播放暂停等操作

  在使用Cocos2d-x播放音乐时需要引入文件SimpleAudioEngine.h(如范例第02行所示),之后就可以使用如范例第42465358行所示的代码来对音乐进行操作了。因为代码非常简单,这里便不再做太多介绍了。

  现在需要读者思考一个问题,为什么在播放音乐时使用的方法是playBackgroundMusic而不是playMusicBackground是背景的意思,是不是说这个方法只能用来播放背景音乐?那么什么音乐不是背景音乐呢?

  实际上该方法是可以播放任何音乐的,但是比较适合播放大段的音乐,而在游戏中大段的音乐常常被用来作为背景音乐使用。在游戏中一些短小的音乐(如怪物的叫声、打斗声等)则是要通过其他方法来播放的,这些内容将在下一节介绍。

211008_KfmG_2633993.png

自学必备!!


转载于:https://my.oschina.net/andyou2011/blog/615909

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值