Skip to content

Commit

Permalink
feat(menu): Update BGM control
Browse files Browse the repository at this point in the history
  • Loading branch information
OctCarp committed Jan 22, 2024
1 parent e43c570 commit 42560de
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
38 changes: 37 additions & 1 deletion Classes/MainMenu/SysMenuScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

USING_NS_CC;

bool SysMenuScene::music_on = true;

Scene* SysMenuScene::createScene()
{
return SysMenuScene::create();
Expand All @@ -54,6 +56,8 @@ bool SysMenuScene::init()
return false;
}

SysMenuScene::music_on = true;

auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();

Expand Down Expand Up @@ -147,14 +151,46 @@ bool SysMenuScene::init()
this->addChild(sprite, 0);
}

Label* musOnLabel = Label::createWithTTF("Mus On", "fonts/Marker Felt.ttf", 48);
Label* musOffLabel = Label::createWithTTF("Mus Off", "fonts/Marker Felt.ttf", 48);
MenuItemToggle* netItem = MenuItemToggle::createWithCallback([&](Ref* sender) {
this->music_on = !this->music_on;
if (this->music_on) {
int bgmId = AudioEngine::play2d("Music/BGM.mp3", true);
}
else {
cocos2d::AudioEngine::stopAll();
}
},
MenuItemLabel::create(musOnLabel), MenuItemLabel::create(musOffLabel), NULL);
Menu* netMenu = Menu::create(netItem, nullptr);
netMenu->setPosition(Vec2(visibleSize.width - 100, visibleSize.height - 48));
this->addChild(netMenu, 1);

//else {
// MenuItemToggle* netItem = MenuItemToggle::createWithCallback([&](Ref* sender) {
// this->music_on = !this->music_on;
// if (this->music_on) {
// int bgmId = AudioEngine::play2d("Music/BGM.mp3", true);
// }
// else {
// cocos2d::AudioEngine::stopAll();
// }
// },
// MenuItemLabel::create(musOffLabel), MenuItemLabel::create(musOnLabel), NULL);
// Menu* netMenu = Menu::create(netItem, nullptr);
// netMenu->setPosition(Vec2(visibleSize.width - 100, visibleSize.height - 48));
// this->addChild(netMenu, 1);
//}

// 停止并释放背景音乐
cocos2d::AudioEngine::stopAll();

// 预加载背景音乐
AudioEngine::preload("Music/BGM.mp3");

// 播放背景音乐,-1 表示无限循环
int bgmId = AudioEngine::play2d("Music/BGM.mp3", true);
// 播放背景音乐,-1 表示无限循环

return true;
}
2 changes: 2 additions & 0 deletions Classes/MainMenu/SysMenuScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class SysMenuScene : public cocos2d::Scene

virtual bool init();

static bool music_on;

// implement the "static create()" method manually
CREATE_FUNC(SysMenuScene);

Expand Down

0 comments on commit 42560de

Please sign in to comment.