Cocos2d-x实例:设置背景音乐与音效-HelloWorld场景实现
HelloWorld场景就是游戏中的主菜单场景。HelloWorld.h文件代码如下:
- #define __HELLOWORLD_SCENE_H__
- #include "cocos2d.h"
- #include "SimpleAudioEngine.h" ①
- #include "SettingScene.h"
- class HelloWorld : public cocos2d::Layer
- {
- public:
- static cocos2d::Scene* createScene();
- virtual bool init(); ②
- virtual void onEnter();
- virtual void onEnterTransitionDidFinish();
- virtual void onExit();
- virtual void onExitTransitionDidStart();
- virtual void cleanup(); ③
- void menuItemHelpCallback(cocos2d::Ref* pSender);
- void menuItemStartCallback(cocos2d::Ref* pSender);
- void menuItemSettingCallback(cocos2d::Ref* pSender);
- CREATE_FUNC(HelloWorld);
- };
- bool HelloWorld::init()
- {
- if ( !Layer::init() )
- {
- return false;
- }
- log("HelloWorld init");
- Size visibleSize = Director::getInstance()->getVisibleSize();
- Point origin = Director::getInstance()->getVisibleOrigin();
- Sprite *bg = Sprite::create("background.png");
- bg->setPosition(Point(origin.x + visibleSize.width/2,
- origin.y + visibleSize.height /2));
- this->addChild(bg);
- //开始精灵
- Sprite *startSpriteNormal = Sprite::create("start-up.png");
- Sprite *startSpriteSelected = Sprite::create("start-down.png");
- MenuItemSprite *startMenuItem = MenuItemSprite::create(startSpriteNormal,
- startSpriteSelected,
- CC_CALLBACK_1(HelloWorld::menuItemStartCallback, this));
- startMenuItem->setPosition(Director::getInstance()->convertToGL(Point(700, 170)));
- // 设置图片菜单
- MenuItemImage *settingMenuItem = MenuItemImage::create(
- "setting-up.png",
- "setting-down.png",
- CC_CALLBACK_1(HelloWorld::menuItemSettingCallback, this));
- settingMenuItem->setPosition(Director::getInstance()->convertToGL(Point(480, 400)));
- // 帮助图片菜单
- MenuItemImage *helpMenuItem = MenuItemImage::create(
- "help-up.png",
- "help-down.png",
- CC_CALLBACK_1(HelloWorld::menuItemHelpCallback, this));
- helpMenuItem->setPosition(Director::getInstance()->convertToGL(Point(860, 480)));
- Menu* mu = Menu::create(startMenuItem, settingMenuItem, helpMenuItem, NULL);
- mu->setPosition(Point::ZERO);
- this->addChild(mu);
- return true;
- }
- void HelloWorld::menuItemSettingCallback(Ref* pSender)
- {
- auto sc = Setting::createScene();
- auto reScene = TransitionJumpZoom::create(1.0f, sc);
- Director::getInstance()->pushScene(reScene);
- SimpleAudioEngine::getInstance()->playEffect("sound/Blip.wav"); ②
- }
- void HelloWorld::menuItemHelpCallback(Ref* pSender)
- {
- MenuItem* item = (MenuItem*)pSender;
- log("Touch Help %p", item);
- SimpleAudioEngine::getInstance()->playEffect("sound/Blip.wav"); ③
- }
- void HelloWorld::menuItemStartCallback(Ref* pSender)
- {
- MenuItem* item = (MenuItem*)pSender;
- log("Touch Start %p", item);
- SimpleAudioEngine::getInstance()->playEffect("sound/Blip.wav"); ④
- }
- void HelloWorld::onEnter()
- {
- Layer::onEnter();
- log("HelloWorld onEnter");
- }
- void HelloWorld::onEnterTransitionDidFinish()
- {
- Layer::onEnterTransitionDidFinish();
- log("HelloWorld onEnterTransitionDidFinish");
- //播放
- SimpleAudioEngine::getInstance()->playBackgroundMusic("sound/Jazz.mp3", true); ⑤
- }
- void HelloWorld::onExit()
- {
- Layer::onExit();
- log("HelloWorld onExit");
- }
- void HelloWorld::onExitTransitionDidStart()
- {
- Layer::onExitTransitionDidStart();
- log("HelloWorld onExitTransitionDidStart");
- }
- void HelloWorld::cleanup()
- {
- Layer::cleanup();
- log("HelloWorld cleanup");
- //停止
- SimpleAudioEngine::getInstance()->stopBackgroundMusic("sound/Jazz.mp3"); ⑥
- }
上述代码第①行using namespace CocosDenshion是使用命名空间CocosDenshion,它是CocosDenshion引擎所需要的。代码②、③、④行SimpleAudioEngine::getInstance()->playEffect("sound/Blip.wav")是在点击菜单时候播放音效。第⑤行代码是onEnterTransitionDidFinish()函数中播放背景音乐。第⑥行代码是cleanup()函数中停止播放背景音乐。
更多内容请关注Cocos2d-x系列图书《Cocos2d-x实战(卷Ⅰ):C++开发》
本书交流讨论网站:http://www.cocoagame.net
欢迎加入cocos2d-x技术讨论群:257760386、327403678