Cocos2d-x实例:设置背景音乐与音效-HelloWorld场景实现

crazymens 2014-08-05

HelloWorld场景就是游戏中的主菜单场景。HelloWorld.h文件代码如下:

[html] view plaincopyCocos2d-x实例:设置背景音乐与音效-HelloWorld场景实现Cocos2d-x实例:设置背景音乐与音效-HelloWorld场景实现
 
  1. #define __HELLOWORLD_SCENE_H__  
  2.   
  3.   
  4. #include "cocos2d.h"  
  5. #include "SimpleAudioEngine.h"                                          ①  
  6. #include "SettingScene.h"  
  7.   
  8.   
  9. class HelloWorld : public cocos2d::Layer  
  10. {  
  11. public:  
  12.     static cocos2d::Scene* createScene();  
  13.   
  14.   
  15.     virtual bool init();                                                    ②  
  16.     virtual void onEnter();  
  17.     virtual void onEnterTransitionDidFinish();  
  18.     virtual void onExit();  
  19.     virtual void onExitTransitionDidStart();  
  20.     virtual void cleanup();                                                 ③  
  21.   
  22.   
  23.     void menuItemHelpCallback(cocos2d::Ref* pSender);  
  24.     void menuItemStartCallback(cocos2d::Ref* pSender);  
  25.     void menuItemSettingCallback(cocos2d::Ref* pSender);  
  26.     CREATE_FUNC(HelloWorld);  
  27. };  
  28.   
  29.   
  30.   
  31.   
  32. bool HelloWorld::init()  
  33. {  
  34.     if ( !Layer::init() )  
  35.     {  
  36.         return false;  
  37.     }  
  38.   
  39.   
  40.     log("HelloWorld init");  
  41.   
  42.   
  43.     Size visibleSize = Director::getInstance()->getVisibleSize();  
  44.     Point origin = Director::getInstance()->getVisibleOrigin();  
  45.   
  46.   
  47.     Sprite *bg = Sprite::create("background.png");  
  48.   
  49.   
  50.     bg->setPosition(Point(origin.x + visibleSize.width/2,  
  51.         origin.y + visibleSize.height /2));  
  52.     this->addChild(bg);  
  53.   
  54.   
  55.     //开始精灵  
  56.     Sprite *startSpriteNormal = Sprite::create("start-up.png");  
  57.     Sprite *startSpriteSelected = Sprite::create("start-down.png");  
  58.   
  59.   
  60.     MenuItemSprite *startMenuItem = MenuItemSprite::create(startSpriteNormal,   
  61.         startSpriteSelected,  
  62.         CC_CALLBACK_1(HelloWorld::menuItemStartCallback, this));  
  63.     startMenuItem->setPosition(Director::getInstance()->convertToGL(Point(700, 170)));  
  64.   
  65.   
  66.     // 设置图片菜单  
  67.     MenuItemImage *settingMenuItem = MenuItemImage::create(  
  68.         "setting-up.png",  
  69.         "setting-down.png",  
  70.         CC_CALLBACK_1(HelloWorld::menuItemSettingCallback, this));   
  71.     settingMenuItem->setPosition(Director::getInstance()->convertToGL(Point(480, 400)));  
  72.   
  73.   
  74.     // 帮助图片菜单  
  75.     MenuItemImage *helpMenuItem = MenuItemImage::create(  
  76.         "help-up.png",  
  77.         "help-down.png",  
  78.         CC_CALLBACK_1(HelloWorld::menuItemHelpCallback, this));   
  79.     helpMenuItem->setPosition(Director::getInstance()->convertToGL(Point(860, 480)));  
  80.   
  81.   
  82.     Menu* mu = Menu::create(startMenuItem, settingMenuItem, helpMenuItem, NULL);      
  83.     mu->setPosition(Point::ZERO);  
  84.     this->addChild(mu);  
  85.   
  86.   
  87.     return true;  
  88. }  
  89.   
  90.   
  91. void HelloWorld::menuItemSettingCallback(Ref* pSender)  
  92. {     
  93.     auto sc = Setting::createScene();  
  94.     auto reScene = TransitionJumpZoom::create(1.0f, sc);  
  95.     Director::getInstance()->pushScene(reScene);  
  96.     SimpleAudioEngine::getInstance()->playEffect("sound/Blip.wav");                  ②  
  97. }  
  98.   
  99.   
  100. void HelloWorld::menuItemHelpCallback(Ref* pSender)  
  101. {  
  102.     MenuItem* item = (MenuItem*)pSender;  
  103.     log("Touch Help %p", item);  
  104.     SimpleAudioEngine::getInstance()->playEffect("sound/Blip.wav");                  ③  
  105. }  
  106.   
  107.   
  108. void HelloWorld::menuItemStartCallback(Ref* pSender)  
  109. {  
  110.     MenuItem* item = (MenuItem*)pSender;  
  111.     log("Touch Start %p", item);  
  112.     SimpleAudioEngine::getInstance()->playEffect("sound/Blip.wav");                  ④  
  113. }  
  114.   
  115.   
  116. void HelloWorld::onEnter()  
  117. {  
  118.     Layer::onEnter();  
  119.     log("HelloWorld onEnter");        
  120. }  
  121.   
  122.   
  123. void HelloWorld::onEnterTransitionDidFinish()  
  124. {  
  125.     Layer::onEnterTransitionDidFinish();  
  126.     log("HelloWorld onEnterTransitionDidFinish");  
  127.     //播放  
  128.     SimpleAudioEngine::getInstance()->playBackgroundMusic("sound/Jazz.mp3", true);       ⑤  
  129. }  
  130.   
  131.   
  132. void HelloWorld::onExit()  
  133. {  
  134.     Layer::onExit();  
  135.     log("HelloWorld onExit");  
  136. }  
  137.   
  138.   
  139. void HelloWorld::onExitTransitionDidStart()  
  140. {  
  141.     Layer::onExitTransitionDidStart();  
  142.     log("HelloWorld onExitTransitionDidStart");  
  143. }  
  144.   
  145.   
  146. void HelloWorld::cleanup()  
  147. {  
  148.     Layer::cleanup();  
  149.     log("HelloWorld cleanup");    
  150.     //停止  
  151.     SimpleAudioEngine::getInstance()->stopBackgroundMusic("sound/Jazz.mp3");         ⑥  
  152. }  

上述代码第①行using namespace CocosDenshion是使用命名空间CocosDenshion,它是CocosDenshion引擎所需要的。代码②、③、④行SimpleAudioEngine::getInstance()->playEffect("sound/Blip.wav")是在点击菜单时候播放音效。第⑤行代码是onEnterTransitionDidFinish()函数中播放背景音乐。第⑥行代码是cleanup()函数中停止播放背景音乐。

更多内容请关注最新Cocos图书《Cocos2d-x实战 C++卷》
本书交流讨论网站:http://www.cocoagame.net
更多精彩视频课程请关注智捷课堂Cocos课程:http://v.51work6.com
欢迎加入Cocos2d-x技术讨论群:257760386


《Cocos2d-x实战 C++卷》现已上线,各大商店均已开售:

京东:http://item.jd.com/11584534.html

亚马逊:http://www.amazon.cn/Cocos2d-x%E5%AE%9E%E6%88%98-C-%E5%8D%B7-%E5%85%B3%E4%B8%9C%E5%8D%87/dp/B00PTYWTLU

当当:http://product.dangdang.com/23606265.html

互动出版网:http://product.china-pub.com/3770734

《Cocos2d-x实战 C++卷》源码及样章下载地址:

源码下载地址:http://51work6.com/forum.php?mod=viewthread&tid=1155&extra=page%3D1 

样章下载地址:http://51work6.com/forum.php?mod=viewthread&tid=1157&extra=page%3D1

欢迎关注智捷iOS课堂微信公共平台
Cocos2d-x实例:设置背景音乐与音效-HelloWorld场景实现

相关推荐