懒骨头(http://blog.csdn.net/iamlazybone QQ:124774397 )
《Flappy Bird》
关于这个游戏骨头不多说了<喎�"http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+1rG907+qyrzRp8+wsMmjqMm91a+yu7rDzP2jqTwvcD4KPHA+1f26w8ewts7Ksbzkv7TBy7y4uPZERU1PxMPV4rj2087Pt8G3wbfK1jwvcD4KPHA+v6q446OhPC9wPgo8cD48YnI+CjwvcD4KPHA+sai7t76zo7o8L3A+CjxwPnZzMjAxMyYjNDM7Y29jb3MyZHgzLjBiZXRhMjxicj4KPC9wPgo8cD7K18/Iz8LU2GFwa6Os1dK1vdfK1LTOxLz+o6yyw7z0vLi49s28xqyjujwvcD4KPHA+PGltZyBzcmM9"http://www.2cto.com/uploadfile/Collfiles/20140211/2014021108530418.jpg" alt="\">
用脚本新建一个空的Cocos2dx项目
新建一个Scene类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#include
"cocos2d.h"
#include
"Obstacle.h"
class
FlyBirdGame :
public
cocos2d::Layer
{
public
:
static
cocos2d::Scene* createScene();
virtual bool init();
CREATE_FUNC(FlyBirdGame);
void
initUI();
void
gameStart(Object* pSender);
void
update(
float
time);
Obstacle* obstacle;
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
#include
"cocos2d.h"
#include
"FlyBirdGame.h"
#include
"resource.h"
;
USING_NS_CC;
Scene* FlyBirdGame::createScene()
{
auto scene = Scene::create();
auto layer = FlyBirdGame::create();
scene->addChild(layer);
return
scene;
}
bool FlyBirdGame::init()
{
if
(!Layer::init())
{
return
false
;
}
initUI();
return
true
;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
// win size
auto winSize = Director::getInstance()->getVisibleSize();
// game bg
auto bg = Sprite::create(bird_bg);
bg->setPosition(winSize.width /
2
, winSize.height /
2
);
bg->setScale(winSize.width / bg->getContentSize().width, winSize.height / bg->getContentSize().height);
this
->addChild(bg);
// start btn
auto startBtn = MenuItemImage::create(bird_start_btn, bird_start_btn_pressed, CC_CALLBACK_1(FlyBirdGame::gameStart,
this
));
auto menu = Menu::create(startBtn, NULL);
menu->setTag(
100
);
this
->addChild(menu);
// hero
auto hero = Sprite::create(bird_hero);
hero->setPosition(winSize.width /
5
, winSize.height*
0.8
);
hero->setVisible(
false
);
hero->setTag(
200
);
this
->addChild(hero);
|
开始游戏按钮绑定的gameStart方法:
1
2
3
4
5
6
7
8
|
void
FlyBirdGame::gameStart(Object* pSender)
{
auto btn =
this
->getChildByTag(
100
);
btn->setVisible(
false
);
auto hero =
this
->getChildByTag(
200
);
Size win = Director::getInstance()->getWinSize();
obstacle->gameStart =
true
;
}
|
还有更新方法:
1
2
3
4
5
|
scheduleUpdate();
void
FlyBirdGame::update(
float
time)
{
obstacle->update();
}
|
水管类:Obstacle.cpp
update方法里判断游戏是游戏是否开始
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
void
Obstacle::update()
{
if
(gameStart ==
false
)
return
;
addCount++;
if
(addCount ==
60
)
{
addOne(
0
);
addCount =
0
;
}
for
(
int
i = obstacleList->count() -
1
; i >=
0
; i--)
{
auto s = (Sprite*)obstacleList->getObjectAtIndex(i);
s->setPositionX(s->getPositionX() -
3
);
if
(s->getPositionX() < -s->getContentSize().width /
2
)
{
obstacleList->removeObjectAtIndex(i);
this
->removeChild(s);
}
}
}
|
并且遍历水管列表
出边界的化销毁
接下来是addOne方法:添加水管方法:
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
|
void
Obstacle::addOne(
int
offsetX)
{
Size size = Director::getInstance()->getWinSize();
auto sprite = Sprite::create(bird_obstacle_up);
Size spriteSize = sprite->getContentSize();
obstacleList->addObject(sprite);
this
->addChild(sprite);
auto sprite2 = Sprite::create(bird_obstacle_down);
Size spriteSize2 = sprite->getContentSize();
obstacleList->addObject(sprite2);
this
->addChild(sprite2);
// set positon
int
maxUpY = size.height + spriteSize.height /
4
;
int
minUpY = size.height - spriteSize.height /
4
;
int
y1 = CCRANDOM_0_1()*(maxUpY - minUpY) + minUpY;
int
maxDownY = spriteSize.height /
4
;
int
minDownY = -spriteSize.height /
4
;
int
y2 = CCRANDOM_0_1()*(maxDownY - minDownY) + minDownY;
if
(y1 - y2 - spriteSize.height <
160
)
{
y2 = y1 - spriteSize.height -
160
;
}
sprite->setPosition(ccp(size.width + spriteSize.width /
2
+ offsetX, y1));
sprite2->setPosition(ccp(size.width + spriteSize2.width /
2
+ offsetX, y2));
}
|
然后随机一下,并且保证上下连个水管有个最小的距离
效果如下:
此时的游戏还没触摸和碰撞逻辑
马上添加:(刚才抽空玩了把魔方:五阶的我只能搞定一个面,虽然有官方规律但是那样好像比的是记忆力)
听说cocos2dx3.0的事件监听方式改变了
先在FlyBirdGame.h里声明俩方法:
1
2
|
void
onTouchesEnded(
const
vector<touch*>& touches, Event* event);
void
onTouchesBegan(
const
vector<touch*>& touches, Event* event);</touch*></touch*>
|
1
2
3
4
5
6
|
// touch
auto dispatcher = Director::getInstance()->getEventDispatcher();
auto listener = EventListenerTouchAllAtOnce::create();
listener->onTouchesEnded = CC_CALLBACK_2(FlyBirdGame::onTouchesEnded,
this
);
listener->onTouchesBegan = CC_CALLBACK_2(FlyBirdGame::onTouchesBegan,
this
);
dispatcher->addEventListenerWithSceneGraphPriority(listener,
this
);
|
(哲哲喊我休息了,先到这吧,待续。。。)