-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEndLayer.cpp
158 lines (144 loc) · 4.73 KB
/
EndLayer.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
#include "EndLayer.h"
#include "HelloWorldScene.h"
#include "GameWin.h"
EndLayer::EndLayer():
hazeCount(0),
catchedDrop(false)
{}
Scene *EndLayer::createScene()
{
auto scene = Scene::create();
auto layer = EndLayer::create();
scene->addChild(layer);
return scene;
}
bool EndLayer::init(){
if (!Layer::init()) {
return false;
}
//背景
Size screenSize = Director::sharedDirector()->getVisibleSize();
end = Sprite::create("main_game/bg_fail.png");
end->setPosition(ccp(screenSize.width/2,screenSize.height/2));
this->addChild(end);
//关卡
level = LabelTTF::create("0", "fonts/msyh.ttf", 60);
level->setPosition(screenSize.width/2+80,screenSize.height-58);
level->setColor(Color3B(232,150,121));
end->addChild(level);
//指示语
prompt = LabelTTF::create("YOU ARE STOPPED HERE !","fonts/msyh.ttf",60);
prompt->setPosition(screenSize.width/2,screenSize.height-138);
prompt->setColor(Color3B(232,150,121));
end->addChild(prompt);
//分数
end_score = LabelTTF::create("distance", "fonts/jokerman.ttf", 48);
end_score->setPosition(screenSize.width/2+100,screenSize.height/2+100);
end_score->setColor(Color3B(139, 248, 178));
end->addChild(end_score);
target = LabelTTF::create("target", "fonts/jokerman.ttf", 48);
target->setPosition(screenSize.width/2+100,screenSize.height/2-40);
target->setColor(Color3B(255,209,27));
end->addChild(target);
//响应键盘消息
auto keyListener = EventListenerKeyboard::create();
keyListener->onKeyReleased = CC_CALLBACK_2(EndLayer::onKeyReleased, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(keyListener, this);
return true;
}
//根据win的真假设置背景和按钮
void EndLayer::setNext(bool win , unsigned n_lev=0 , unsigned n_tar=0) {
Size screenSize = Director::sharedDirector()->getVisibleSize();
MenuItemImage* btn_to;
if (win)
{
//开启hazeUpdate
this->schedule(schedule_selector(EndLayer::hazeUpdate),0.02,14,0.05);
end->setTexture(TextureCache::sharedTextureCache()->addImage("main_game/bg_success.png"));
btn_to = MenuItemImage::create(
"main_game/next1.png",
"main_game/next2.png",
CC_CALLBACK_1(EndLayer::NextFunc, this));
this->n_level = n_lev;
this->n_target = n_tar;
//胜利界面不需要分数和目标
end->removeChild(target,false);
end->removeChild(end_score,false);
//this->target->setString("100");
//this->end_score->setString("100");
//显示haze
hazeCount = 15;
auto h=n_level<7?100-(n_level-1)*15:0;
haze = LabelTTF::create(CCString::createWithFormat("%d",h+hazeCount)->getCString(),"fonts/scoreboard.ttf",80);
haze->setColor(Color3B::RED);
haze->setPosition(screenSize.width/2+50,screenSize.height/2+50);
end->addChild(haze);
}
else{
btn_to = MenuItemImage::create(
"main_game/retry1.png",
"main_game/retry2.png",
CC_CALLBACK_1(EndLayer::RetryFunc, this));
}
btn_to->setPosition(screenSize.width/2 - btn_to->getContentSize().width,btn_to->getContentSize().height/2);
//按钮Home
auto btn_home = MenuItemImage::create(
"main_game/home1.png",
"main_game/home2.png",
CC_CALLBACK_1(EndLayer::ReturnToMainFunc, this));
btn_home->setPosition(screenSize.width/2 + btn_home->getContentSize().width,btn_home->getContentSize().height/2);
auto menu = Menu::create(btn_to,btn_home,NULL);
menu->setPosition(0,0);
end->addChild(menu);
}
void EndLayer::hazeUpdate(float dt){
if (this->hazeCount > 0)
{
hazeCount--;
haze->setString(CCString::createWithFormat("%d",100-(n_level-1)*15+hazeCount)->getCString());
}
else
{
return;
}
}
void EndLayer::NextFunc(cocos2d::Ref* pSender){
//下一关
int nextHaze = 210 - 30 * (n_level - 1);
if (nextHaze <= 0){
Scene* newScene = GameWin::createScene();
GameWin* layer = (GameWin*)(newScene->getChildByTag(13));
Director::sharedDirector()->replaceScene(newScene);
}
else{
Scene* newScene = PlayLayer::createScene();
PlayLayer* layer = (PlayLayer*)(newScene->getChildByTag(13));
//设定下一关的初始值
if (catchedDrop){
layer->getProbar()->setPercentage(100);
}
layer->setLevel(this->n_level);
layer->setTarget(this->n_target);
layer->getHaze()->setOpacity(nextHaze);
layer->setPhyWorld(newScene->getPhysicsWorld());
Director::sharedDirector()->replaceScene(newScene);
}
}
void EndLayer::RetryFunc(cocos2d::Ref* pSender){
CCDirector::sharedDirector()->replaceScene(PlayLayer::createScene());
}
void EndLayer::ReturnToMainFunc(cocos2d::Ref* pSender){
auto newScene = HelloWorld::createScene();
CCDirector::sharedDirector()->replaceScene(CCTransitionCrossFade::create(0.2,newScene));
}
//响应安卓返回键
void EndLayer::onKeyReleased(EventKeyboard::KeyCode keycode,Event* event)
{
if(keycode == EventKeyboard::KeyCode::KEY_BACKSPACE)
{
Director::sharedDirector()->replaceScene(CCTransitionCrossFade::create(0.2,HelloWorld::createScene()));
}
}
void EndLayer::setCatchedDrop(){
this->catchedDrop = true;
}