-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathManager.cpp
151 lines (140 loc) · 3.89 KB
/
Manager.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
#include "Manager.h"
#include "PlayLayer.h"
USING_NS_CC;
bool Manager::init(){
if (!Node::init()){
return false;
}
frameCache = SpriteFrameCache::getInstance();
frameCache->addSpriteFramesWithFile("hazefight.plist", "hazefight.png");
createBarrier();
createBug();
createCoin();
this->height = 60;
this->scheduleUpdate();
return true;
}
void Manager::createCoin(){
for (int i = 0; i < 5; ++i){
auto coin = Base::create();
coin->sprite = Sprite::createWithSpriteFrameName("waterDrop0.png");
coin->addChild(coin->sprite);
coin->sprite->runAction(coin->createAnimate(frameCache,4,"waterDrop"));
coin->initBody(0);
coin->setVisible(false);
coin->setTag(coinTag);
this->addChild(coin);
this->m_coin.pushBack(coin);
}
}
void Manager::createBarrier(){
for (int i = 0; i < 5; ++i){
auto barr = Base::create();
//barr->sprite = Sprite::create("barrier.png");
barr->sprite = Sprite::createWithSpriteFrameName("barrier.png");
barr->addChild(barr->sprite);
barr->initBody(0xFF);
//barr->initBody(1);
barr->setVisible(false);
barr->setTag(barrTag);
this->addChild(barr);
this->m_barr.pushBack(barr);
}
}
void Manager::update(float dt){
int setNum = 0;
for (Vector<Base*>::iterator iter = this->m_coin.begin(); iter != this->m_coin.end(); ++iter){
Base* coin = *iter;
//不在屏幕
if (coin->getPositionX() < -coin->getContentSize().width ){
coin->setVisible(false);
}
//要是不可见,要不就是不在屏幕里,要不就是被碰撞了
if (!coin->isVisible()){
setNum++;
}
//让金币移动
coin->setPositionX(coin->getPositionX() - BACKGROUND_SPEED);
}
if (setNum == 5){
int i = 0;
float posX = WIDTH+400;
float posY = height + CCRANDOM_0_1() * INTERVAL;
for (Vector<Base*>::iterator iter = this->m_coin.begin(); iter != this->m_coin.end(); ++iter){
Base* coin = *iter;
//两个两个一起
if (i < 2){
posX += 100;//两个金币同一高度,间隔100
}
else{
//重新重置位置
i = -1;
posY = height + CCRANDOM_0_1() * INTERVAL;
posX += WIDTH;
}
i++;
coin->setVisible(true);
coin->setPosition(posX, posY);
if (i == 0){
auto bug = *(m_bug.begin() + 1);
bug->setPosition(posX, posY + 30);
bug->setVisible(true);
}
}
}
setNum = 0;
//障碍物
for (Vector<Base*>::iterator iter = this->m_barr.begin(); iter != this->m_barr.end(); ++iter){
Base* barr = *iter;
if (barr->getPositionX() < -barr->getContentSize().width-100)
barr->setVisible(false);
if (!barr->isVisible())
setNum++;
//比背景移动的快一倍
barr->setPositionX(barr->getPositionX() - (2+BACKGROUND_SPEED));
}
if (setNum == 5){
float posX = WIDTH+400;
float posY;
int i = 0;
for (Vector<Base*>::iterator iter = this->m_barr.begin(); iter != this->m_barr.end(); ++iter,++i){
auto barr = *iter;
posX += WIDTH;
posY = height + CCRANDOM_0_1() * INTERVAL;
if (i == 3){
auto bug = *(m_bug.begin());
bug->setPosition(posX, posY+160);
bug->setVisible(true);
}
barr->setPosition(posX, posY);
barr->setVisible(true);
}
}
//bug移动
for (Vector<Base*>::iterator iter = this->m_bug.begin(); iter != this->m_bug.end(); ++iter){
auto bug = *iter;
if (bug->getPositionX() < -bug->getContentSize().width - 100){
bug->setVisible(false);
}
else
bug->setPositionX(bug->getPositionX() - (4 + BACKGROUND_SPEED));
}
}
void Manager::createBug(){
for (int i = 0; i < 2; ++i){
auto bug = Base::create();
bug->sprite = Sprite::createWithSpriteFrameName("bug0.png");
bug->addChild(bug->sprite);
bug->sprite->runAction(bug->createAnimate(frameCache, 4, "bug"));
auto phyBody = PhysicsBody::createBox(bug->sprite->getContentSize());
phyBody->getShape(0)->setMaterial(PHYSICSBODY_MATERIAL_DEFAULT);
phyBody->setCollisionBitmask(0x02);
phyBody->setContactTestBitmask(1);
phyBody->setRotationEnable(false);
bug->setPhysicsBody(phyBody);
bug->setVisible(false);
bug->setTag(bugTag);
this->addChild(bug);
this->m_bug.pushBack(bug);
}
}