-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStartScene.cpp
109 lines (88 loc) · 2.84 KB
/
StartScene.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "StartScene.h"
#include"SceneMediator.h"
#include "SimpleAudioEngine.h"
USING_NS_CC;
using namespace CocosDenshion;
StartScene::StartScene():
_title(nullptr),
_startBtn(nullptr),
_scoreBtn(nullptr),
_aboutBtn(nullptr)
{
}
StartScene::~StartScene()
{
}
bool StartScene::init()
{
if (Layer::init() == false){ return false; }
_background = Background::create();
addChild(_background);
auto viewSize = Director::getInstance()->getVisibleSize();
_title = Sprite::create("aipin.png");
_title->setPosition(viewSize.width / 2, viewSize.height - 100);
addChild(_title);
_startBtn = ui::Button::create("start.png");
_startBtn->setPosition(Vec2(viewSize.width / 2, viewSize.height / 2 ));
addChild(_startBtn);
_scoreBtn = ui::Button::create("charts.png");
_scoreBtn->setPosition(Vec2(viewSize.width / 2, viewSize.height / 2 -100));
addChild(_scoreBtn);
_aboutBtn = ui::Button::create("about.png");
_aboutBtn->setPosition(Vec2(viewSize.width / 2, viewSize.height / 2 - 200));
addChild(_aboutBtn);
_endBtn = ui::Button::create("end_game.png");
_endBtn->setPosition(Vec2(viewSize.width / 2, viewSize.height / 2 - 300));
addChild(_endBtn);
_startBtn->addClickEventListener([](Ref* ref){
SceneMediator::getInstance()->gotoGameScene();
});
_scoreBtn->addClickEventListener([](Ref* ref){
SceneMediator::getInstance()->gotoScoreScene();
});
_aboutBtn->addClickEventListener([](Ref* ref){
SceneMediator::getInstance()->gotoaboutScene();
});
_endBtn->addClickEventListener([](Ref* ref){
Director::getInstance()->end();
});
// 添加 声音 的开关按钮
auto _turnOn = MenuItemImage::create(
"button_voi_on.png",
"button_voi_on.png");
auto _turnOff = MenuItemImage::create(
"button_voi_off.png",
"button_voi_off.png");
MenuItemToggle *toggleItem = MenuItemToggle::createWithCallback(CC_CALLBACK_1(StartScene::menuMusicCallback, this), _turnOn, _turnOff, NULL);
toggleItem->setScale(0.3f);
toggleItem->setPosition(Point(viewSize.width-50, 50));
auto menu = Menu::create(toggleItem, NULL);
menu->setPosition(Point::ZERO);
this->addChild(menu);
// 预加载背景音乐和音效
SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic("music/aipin.mp3");
// 先默认播放背景音乐
SimpleAudioEngine::sharedEngine()->playBackgroundMusic("music/aipin.mp3", true);
SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(0.8);
isPause = false;
}
Scene* StartScene::createScene()
{
auto layer = StartScene::create();
auto scene = Scene::create();
scene->addChild(layer);
return scene;
}
void StartScene::menuMusicCallback(cocos2d::Ref* pSender)
{
if (isPause == false)
{
SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
isPause = true;
}
else
{
SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
isPause = false;
}
}