-
Notifications
You must be signed in to change notification settings - Fork 1
/
HelloWorldScene.cpp
169 lines (155 loc) · 4.81 KB
/
HelloWorldScene.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "HelloWorldScene.h"
#include "About.h"
#define userDefault UserDefault::getInstance()
USING_NS_CC;
bool isFirstMusic = true;
Scene* HelloWorld::createScene()
{
auto scene = Scene::create();
auto layer = HelloWorld::create();
scene->addChild(layer);
return scene;
}
bool HelloWorld::init()
{
if ( !Layer::init() )
{
return false;
}
//初始化
touchedBack = 0;
exit = NULL;
Size screenSize = Director::getInstance()->getVisibleSize();
Point origin = Director::getInstance()->getVisibleOrigin();
CCSprite* Background = CCSprite::create("menu/UI1.png");
Background->setPosition(ccp(screenSize.width/2,screenSize.height/2));
this->addChild(Background);
//触摸效果开启
this->setTouchEnabled(true);
//开始游戏
auto item_Adventure = MenuItemImage::create(
"menu/play1.png",
"menu/play2.png",
CC_CALLBACK_1(HelloWorld::startGame, this));
item_Adventure->setPosition(screenSize.width/2+55,screenSize.height/2-28);
//关于界面
auto item_About= MenuItemImage::create(
"menu/about1.png",
"menu/about2.png",
CC_CALLBACK_1(HelloWorld::aboutFunc, this));
item_About->setPosition(item_About->getContentSize().width-20,item_About->getContentSize().height/2);
//播放背景音乐
if(!userDefault->getBoolForKey("Music")){
userDefault->getBoolForKey("Music",true);
userDefault->flush();
}
int musBef = userDefault->getBoolForKey("Music");
userDefault->flush();
if(isFirstMusic & musBef)
{
CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("sound/Babkg_music.wav",true);
isFirstMusic = false;
}
//声音开关
//打开关闭两个状态
auto on = MenuItemImage::create("menu/voice1.png","menu/voice2.png",NULL,NULL);
auto off = MenuItemImage::create("menu/voice3.png","menu/voice4.png",NULL,NULL);
//初始化状态为1
MenuItemToggle *item_Voice;
int selectId;
if (CocosDenshion::SimpleAudioEngine::sharedEngine()->isBackgroundMusicPlaying())
{
item_Voice = MenuItemToggle::createWithTarget(this, menu_selector(HelloWorld::setOptions),on,off, NULL);
AudioState = 1;
selectId = AudioState? 0 : 1;
}
else
{
item_Voice = MenuItemToggle::createWithTarget(this, menu_selector(HelloWorld::setOptions),off,on, NULL);
AudioState = 0;
selectId = AudioState? 1 : 0;
}
//int selectId = AudioState? 0 : 1;
item_Voice->setSelectedIndex(selectId);
item_Voice->setPosition(item_Voice->getContentSize().width-20,item_Voice->getContentSize().height*2);
CCMenu* menu = CCMenu::create(item_Adventure,item_Voice,item_About,NULL);
menu->setPosition(0,0);
this->addChild(menu);
//响应键盘消息
auto keyListener = EventListenerKeyboard::create();
keyListener->onKeyReleased = CC_CALLBACK_2(HelloWorld::onKeyReleased, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(keyListener, this);
// bind touch event实现触摸效果
auto touchListener = EventListenerTouchOneByOne::create();
touchListener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
return true;
}
//关于按键的回调函数
void HelloWorld::aboutFunc(Ref* pSender)
{
CCLOG("about_Menu item clicked");
Director::sharedDirector()->pushScene(GameAbout::createScene());
//Director::sharedDirector()->replaceScene();
}
//开始游戏按键的回调函数
void HelloWorld::startGame(cocos2d::Ref* pSender)
{
CCLOG("dventure_Menu item clicked");
CCDirector::sharedDirector()->replaceScene(PlayLayer::createScene());
}
//声音按键的回调函数,把用户的选择存储在后台
void HelloWorld::setOptions(cocos2d::Ref* pSender)
{
bool temp = userDefault->getBoolForKey("Music");
userDefault->flush();
userDefault->setBoolForKey("Music",!temp);
userDefault->flush();
bool tmpSound = AudioState;
AudioState = !AudioState;
if (!tmpSound) {
CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("sound/Babkg_music.wav",true);
isFirstMusic = false;
}else{
CocosDenshion::SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
}
}
//响应安卓返回键
void HelloWorld::onKeyReleased(EventKeyboard::KeyCode keycode,Event* event)
{
exit = LabelTTF::create("Press back button again to exit", "fonts/MSYH.TTF", 48);
if(keycode == EventKeyboard::KeyCode::KEY_BACKSPACE)
{
if(!touchedBack)
{
Size visibleSize = Director::getInstance()->getVisibleSize();
Point origin = Director::getInstance()->getVisibleOrigin();
exit->runAction(CCFadeOut::create(2.0));
exit->setColor(Color3B::BLACK);
exit->setPosition(Point(origin.x + visibleSize.width/2,
origin.y + visibleSize.height-60));
this->addChild(exit);
touchedBack = true;
}
else
Director::getInstance()->end();
}
else
{
touchedBack = false;
if(exit)
{
exit->removeFromParentAndCleanup(true);
}
}
}
//响应触摸
bool HelloWorld::onTouchBegan(Touch *touch, Event *unused)
{
touchedBack = false;
if(exit)
{
exit->removeFromParentAndCleanup(true);
}
return touchedBack;
}