-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenuScene.cpp
More file actions
149 lines (132 loc) · 4.41 KB
/
Copy pathMenuScene.cpp
File metadata and controls
149 lines (132 loc) · 4.41 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "MenuScene.h"
#include "BreakoutScene.h"
#include "Breakout3Scene.h"
#include "Breakout2Scene.h"
#include "SimpleAudioEngine.h"
#pragma execution_character_set("utf-8")
using namespace CocosDenshion;
USING_NS_CC;
void MenuScene::setPhysicsWorld(PhysicsWorld* world) { m_world = world; }
Scene* MenuScene::createScene()
{
//创建菜单场景
auto scene = Scene::createWithPhysics();
scene->getPhysicsWorld()->setGravity(Point(0, 0));
auto layer = MenuScene::create(scene->getPhysicsWorld());
scene->addChild(layer);
return scene;
}
//预加载开始界面音乐
void MenuScene::ppreloadMusic()
{
SimpleAudioEngine::getInstance()->preloadBackgroundMusic("music/beginmusic.mp3");
}
//播放开始界面音乐
void MenuScene::pplayBgm()
{
SimpleAudioEngine::getInstance()->playBackgroundMusic("music/beginmusic.mp3", true);
}
//物理世界
bool MenuScene::init(PhysicsWorld* world)
{
if ( !Layer::init() )
{
return false;
}
this->setPhysicsWorld(world);
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
//添加背景图片
auto backgroundmap = Sprite::create("Cover.jpg");
backgroundmap->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y));
Size mywinsize = Director::getInstance()->getWinSize();
//获取屏幕宽or高度
float windowswidth = mywinsize.width;
float windowsheight = mywinsize.height;
float spx = backgroundmap->getTextureRect().getMaxX();
float spy = backgroundmap->getTextureRect().getMaxY();
//由屏幕宽or高度设置对应精灵宽度缩放比例
backgroundmap->setScaleX(windowswidth / spx);
backgroundmap->setScaleY(windowsheight / spy);
this->addChild(backgroundmap, -1);
//关闭游戏按钮
auto closeItem = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(MenuScene::menuCloseCallback, this));
closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width / 2 - 10,
origin.y + closeItem->getContentSize().height / 2 + 10));
closeItem->setScale(1.5);
auto menuu = Menu::create(closeItem, NULL);
menuu->setPosition(Vec2::ZERO);
this->addChild(menuu, 1);
//第一关按钮
auto startItem0 = MenuItemImage::create(
"level1.png",
"level11.png",
CC_CALLBACK_1(MenuScene::BreakoutMenuCallback, this));
startItem0->setPosition(Vec2(visibleSize.width / 2 + origin.x - 10, visibleSize.height / 8 * 5 + origin.y -130));
startItem0->setScale(1.5);
auto menu = Menu::create(startItem0, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 3);
//第二关按钮
auto startItem1 = MenuItemImage::create(
"level2.png",
"level22.png",
CC_CALLBACK_1(MenuScene::Breakout2MenuCallback, this));
startItem1->setPosition(Vec2(visibleSize.width / 2 + origin.x -10 , visibleSize.height / 8 * 5 + origin.y - 180));
startItem1->setScale(1.5);
auto menu1 = Menu::create(startItem1, NULL);
menu1->setPosition(Vec2::ZERO);
this->addChild(menu1, 3);
//第三关按钮
auto startItem2 = MenuItemImage::create(
"level3.png",
"level33.png",
CC_CALLBACK_1(MenuScene::Breakout3MenuCallback, this));
startItem2->setPosition(Vec2(visibleSize.width / 2 + origin.x - 10, visibleSize.height / 8 * 5 + origin.y - 230));
startItem2->setScale(1.5);
auto menu2= Menu::create(startItem2, NULL);
menu2->setPosition(Vec2::ZERO);
this->addChild(menu2, 3);
ppreloadMusic();
pplayBgm();
return true;
}
//构建物理世界
MenuScene * MenuScene::create(PhysicsWorld * world)
{
MenuScene* pRet = new(std::nothrow) MenuScene();
if (pRet && pRet->init(world))
{
pRet->autorelease();
return pRet;
}
delete pRet;
pRet = NULL;
return NULL;
}
//跳转到第一关游戏页面
void MenuScene::BreakoutMenuCallback(cocos2d::Ref * pSender)
{
CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(1.5, Breakout::createScene()));
}
//跳转到第二关游戏页面
void MenuScene::Breakout2MenuCallback(cocos2d::Ref * pSender)
{
CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(1.5f, Breakout2::createScene()));
}
//跳转到第三关游戏页面
void MenuScene::Breakout3MenuCallback(cocos2d::Ref * pSender)
{
CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(1.5f, Breakout3::createScene()));
}
//执行退出游戏
void MenuScene::menuCloseCallback(Ref* pSender)
{
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}