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实战 C++卷》现已上线,各大商店均已开售:
京东:http://item.jd.com/11584534.html
当当: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
相关推荐
进入场景而且过渡动画结束时候触发。提示 GameScene场景中的继承于节点,这些生命周期事件根本上是从Node继承而来。事实上所有Node对象都有这些事件,具体实现代码与GameScene场景类似。