Cocos2dx游戏开发笔记21:动手学习《Flappy Bird》(待续)
懒骨头(http://blog.csdn.net/iamlazybone QQ: )
《Flappy Bird》
关于这个游戏骨头不多说了vcD4KPHA+1rG907+qyrzRp8+wsMmjqMm91a+yu7rDzP2jqTwvcD4KPHA+1f26w8ewts7Ksbzkv7TBy7y4uPZERU1PxMPV4rjwbfK1jwvcD4KPHA+v6q446OhPC9wPgo8cD48YnI+CjwvcD4KPHA+sai7t76zo7o8L3A+CjxwPnZzMjAxMyYjNDM7Y29jb3MyZHgzLjBiZXRhMjxicj4KPC9wPgo8cD7K18/Iz8LU2GFwa6Os1dK1vdfK1LTOxLz+o6yyw7z0vLi49s28xqyjujwvcD4KPHA+PGltZyBzcmM9"/uploadfile/Collfiles/8.jpg" alt="\">
用脚本新建一个空的Cocos2dx项目
新建一个Scene类
#include "cocos2d.h"
#include "Obstacle.h"
class FlyBirdGame :public cocos2d::Layer
static cocos2d::Scene* createScene();
virtual bool init();
CREATE_FUNC(FlyBirdGame);
void initUI();
void gameStart(Object* pSender);
void update(float time);
#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);
bool FlyBirdGame::init()
if (!Layer::init())
initUI里是一些UI初始化方法:
// 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);
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方法:
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 =
隐藏开始按钮,显示小鸟,水管开始移动
还有更新方法:
scheduleUpdate();
void FlyBirdGame::update(float time)
obstacle->update();
=======================================
水管类:Obstacle.cpp
update方法里判断游戏是游戏是否开始
void Obstacle::update()
if (gameStart == false)
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() getContentSize().width / 2)
obstacleList->removeObjectAtIndex(i);
this->removeChild(s);
}水管类的更新方法里,每60帧(1秒)添加一对水管
并且遍历水管列表
出边界的化销毁
接下来是addOne方法:添加水管方法:
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 setPosition(ccp(size.width + spriteSize.width / 2 + offsetX, y1));
sprite2->setPosition(ccp(size.width + spriteSize2.width / 2 + offsetX, y2));
}这段代码比较凌乱,就是找到水管上下位置的范围
然后随机一下,并且保证上下连个水管有个最小的距离
效果如下:
=============================
此时的游戏还没触摸和碰撞逻辑
马上添加:(刚才抽空玩了把魔方:五阶的我只能搞定一个面,虽然有官方规律但是那样好像比的是记忆力)
听说cocos2dx3.0的事件***方式改变了
先在FlyBirdGame.h里声明俩方法:
void onTouchesEnded(const vector& touches, Event* event);
void onTouchesBegan(const vector& touches, Event* event);
在cpp文件的初始化里绑定事件:
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);
在两个事件方法里改变标记位,在小鸟的update方法里根据这个标记位来改变高度
(哲哲喊我休息了,先到这吧,待续。。。)
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467142',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'Flappy bird怎么玩?Flappy bird如何玩?_图文攻略_全通关攻略_高分攻略_百度攻略
Flappy bird中文名字也叫像素鸟,该游戏是目前一款非常火爆的像素风格游戏。Flappy bird最初只是出现在IOS平台,后来发展到电脑网页版等等各种版本。但是还是有很多新手玩家不知道Flappy bird怎么玩?Flappy bird如何玩?其实这款游戏的操作真的很简单,下面小编就来告诉大家Flappy bird怎么玩,希望能帮助到大家。Flappy bird的玩法还是很简单的,只是游戏难度比较大。玩家在游戏中只要点击设备屏幕,小鸟就会往上飞跃,但是那只小鸟紧接着又会往下掉,所以你必须每隔一段时间要点击一次屏幕。这个间隔时间大家自己拿捏的要准确,一个不小心小鸟就会撞到水管上面game over了。说到这儿,我想大家对Flappy bird怎么玩,应该有个大概了解了吧。精彩内容,尽在百度攻略:下面小编再透漏一点小小的心得给大家,大家在玩Flappy bird的时候尽量保持心平气和的心态去玩,这样才有可能得高分。如果你越暴躁得分就会越低,甚至有可能把手机也砸了,哈哈!开个玩笑啦!到这儿我想大家应该清楚Flappy bird怎么玩了吧,最后,祝大家都能玩出自己满意的高分。精彩内容,尽在百度攻略:
像素鸟-相关攻略推荐
像素鸟-综合攻略