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

时间:2023-02-08 10:31:39
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()函数中停止播放背景音乐。



更多内容请关注最新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场景实现