-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelloWorldScene.cpp
More file actions
135 lines (113 loc) · 3.42 KB
/
HelloWorldScene.cpp
File metadata and controls
135 lines (113 loc) · 3.42 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* 欢迎游戏界面
* 功能
* 通向设置界面
* 通向游戏选择关卡界面
* 退出程序
*/
#include "HelloWorldScene.h"
#include "Scene/SelectLevelScene.h"
#include "Scene/SetVolumeScene.h"
#include "AudioEngine.h"
USING_NS_CC;
Scene* HelloWorld::createScene()
{
return HelloWorld::create();
}
// Print useful error message instead of segfaulting when files are not there.
static void problemLoading(const char* filename)
{
printf("Error while loading: %s\n", filename);
printf("Depending on how you compiled you might have to add 'Resources/' in front of filenames in HelloWorldScene.cpp\n");
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
if ( !Scene::init() )
{
return false;
}
auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
//BGM
if (AudioEngine::getState(worldBgmId) != AudioEngine::AudioState::PLAYING) {
worldBgmId = AudioEngine::play2d("Resources/Audio/WorldBGM.mp3", true);
}
auto startItem = MenuItemImage::create(
"Resources/HelloWorld/StartNormal.png",
"Resources/HelloWorld/StartSelected.png",
CC_CALLBACK_1(HelloWorld::menuStartCallBack, this));
if (startItem == nullptr ||
startItem->getContentSize().width <= 0 ||
startItem->getContentSize().height <= 0)
{
problemLoading("'StartNormal.png' and 'StartSelected.png'");
}
else
{
startItem->setPosition(Vec2(150.0, 250.0));
}
//退出游戏按钮
auto closeItem = MenuItemImage::create(
"Resources/HelloWorld/CloseNormal.png",
"Resources/HelloWorld/CloseSelected.png",
CC_CALLBACK_1(HelloWorld::menuCloseCallBack, this));
if (closeItem == nullptr ||
closeItem->getContentSize().width <= 0 ||
closeItem->getContentSize().height <= 0)
{
problemLoading("'CloseNormal.png' and 'CloseSelected.png'");
}
else
{
closeItem->setPosition(Vec2(150.0, 150.0));
}
//设置按钮
auto setItem = MenuItemImage::create(
"Resources/HelloWorld/SettingNormal.png",
"Resources/HelloWorld/SettingSelected.png",
CC_CALLBACK_1(HelloWorld::menuSetCallback, this));
float x = origin.x + setItem->getContentSize().width / 2;
float y = origin.y + visibleSize.height - setItem->getContentSize().height / 2;
setItem->setPosition(Vec2(x, y));
auto menu = Menu::create(startItem, closeItem, setItem, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1);
//游戏背景
auto sprite = Sprite::create("Resources/HelloWorld/WorldBG.jpg");
if (sprite == nullptr)
{
problemLoading("'HelloWorld/WorldBG.jpg'");
}
else
{
// position the sprite on the center of the screen
sprite->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y));
// add the sprite as a child to this layer
this->addChild(sprite, 0);
}
return true;
}
//通向选择关卡界面
void HelloWorld::menuStartCallBack(cocos2d::Ref * pSender)
{
AudioEngine::stop(worldBgmId);
auto nextScene = SelectLevel::create();
Director::getInstance()->replaceScene(
TransitionSlideInT::create(1.0f / 60, nextScene));
MenuItem *item = (MenuItem*)pSender;
}
//退出游戏
void HelloWorld::menuCloseCallBack(Ref* pSender)
{
AudioEngine::stopAll();
Director::getInstance()->end();
}
//通向设置界面
void HelloWorld::menuSetCallback(Ref *pSender)
{
auto nextScene = SetVolume::create();
Director::getInstance()->replaceScene(
TransitionSlideInT::create(1.0f / 60, nextScene));
MenuItem *item = (MenuItem*)pSender;
}