Cocos2d-x FlappyBird
HelloWorldScene.cpp
#include "HelloWorldScene.h" USING_NS_CC; CCScene* HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create(); // 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create(); //添加一个背景颜色图层
CCSize s = CCDirector::sharedDirector()->getWinSize(); CCLayer *colorlayer = CCLayerColor::create(ccc4(0x00, 0xff, 0xff, 0xff), s.width, s.height); colorlayer->ignoreAnchorPointForPosition(false); colorlayer->setPosition(s.width / , s.height / ); scene->addChild(colorlayer, , colorlayer->getTag()); // add layer as a child to scene
//将主图层添加都背景图层中
colorlayer->addChild(layer); // return the scene
return scene;
} // on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !CCLayer::init() )
{
return false;
} //取得屏幕的大小
screenSize = CCDirector::sharedDirector()->getVisibleSize(); //初始化世界
initWorld(); //添加小鸟、障碍物容器及地面
addBird();
addBarContainer();
addGround(); //每隔一秒添加一组障碍物
scheduleUpdate();
schedule(schedule_selector(HelloWorld::addBar), 1.0f); //设置允许触摸
setTouchEnabled(true); return true;
} void HelloWorld::initWorld()
{
//重力加速度:纵向9.8,横向 0
world = new b2World(b2Vec2(, -9.8f));
//添加监视
world->SetContactListener(this);
} void HelloWorld::startGame(float dt){
scheduleUpdate();
schedule(schedule_selector(HelloWorld::addBar), );
} void HelloWorld::stopGame(){
unscheduleUpdate();
unschedule(schedule_selector(HelloWorld::addBar));
} void HelloWorld::addBird()
{
bird = B2Sprite::create("bird.png");
CCSize size = bird->getContentSize(); //添加小鸟
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position = b2Vec2(screenSize.width//RATIO, screenSize.height//RATIO);
b2Body * birdBody = world->CreateBody(&bodyDef); //设置边界
b2PolygonShape birdShape;
birdShape.SetAsBox(size.width//RATIO, size.height//RATIO); //碰撞
b2FixtureDef birdFixtureDef;
birdFixtureDef.shape= &birdShape;
birdBody->CreateFixture(&birdFixtureDef); bird->setPTMRatio(RATIO);
bird->setB2Body(birdBody);
addChild(bird);
} void HelloWorld::addBar(float dt)
{
float offset = -rand()%; //downbar
B2Sprite *downBar = B2Sprite::create("down_bar.png");
CCSize downBarSize = downBar->getContentSize(); b2BodyDef downBarBodyDef;
downBarBodyDef.type = b2_kinematicBody;
downBarBodyDef.position = b2Vec2(screenSize.width/RATIO + , downBarSize.height/RATIO/ + offset);
downBarBodyDef.linearVelocity = b2Vec2(-5.0f, ); b2PolygonShape downBarShape;
downBarShape.SetAsBox(downBarSize.width/RATIO/, downBarSize.height/RATIO/); b2FixtureDef downBarFixtureDef;
downBarFixtureDef.shape = &downBarShape; b2Body *downBarBody = world->CreateBody(&downBarBodyDef);
downBarBody->CreateFixture(&downBarFixtureDef); downBar->setB2Body(downBarBody);
downBar->setPTMRatio(RATIO); //upbar
B2Sprite *upBar = B2Sprite::create("up_bar.png");
CCSize upBarSize = upBar->getContentSize(); b2BodyDef upBarBodyDef;
upBarBodyDef.type = b2_kinematicBody;
upBarBodyDef.position = b2Vec2(screenSize.width/RATIO + , upBarSize.height/RATIO/ + offset + downBarSize.height/RATIO + 2.5f);
upBarBodyDef.linearVelocity = b2Vec2(-, ); b2PolygonShape upBarShape;
upBarShape.SetAsBox(upBarSize.width/RATIO/, upBarSize.height/RATIO/); b2FixtureDef upBarFixtureDef;
upBarFixtureDef.shape = &upBarShape; b2Body *upBarBody = world->CreateBody(&upBarBodyDef);
upBarBody->CreateFixture(&upBarFixtureDef); upBar->setB2Body(upBarBody);
upBar->setPTMRatio(RATIO); barContainer->addChild(upBar);
barContainer->addChild(downBar);
} void HelloWorld::addBarContainer()
{
//将所有的障碍物包裹在一个容器中
barContainer = CCSprite::create();
addChild(barContainer);
} void HelloWorld::addGround()
{
B2Sprite *ground = B2Sprite::create("ground.png");
CCSize size = ground->getContentSize(); b2BodyDef bDef;
bDef.type = b2_staticBody;
bDef.position = b2Vec2(size.width//RATIO, size.height//RATIO);
b2Body *groundBody = world->CreateBody(&bDef); b2PolygonShape groundShape;
groundShape.SetAsBox(size.width//RATIO, size.height//RATIO); b2FixtureDef groundFixtureDef;
groundFixtureDef.shape = &groundShape;
groundBody->CreateFixture(&groundFixtureDef); ground->setPTMRatio(RATIO);
ground->setB2Body(groundBody);
addChild(ground);
} void HelloWorld::update(float dt)
{
world->Step(dt, , ); CCSprite *sprite; //取得世界中的所有的body
b2Body *node = world->GetBodyList(); //遍历世界中的body
while(node)
{
b2Body *body = node;
node = node->GetNext();
sprite = (CCSprite*)body->GetUserData(); //超出屏幕左边界,销毁
if (body->GetPosition().x < -)
{
if (sprite != NULL)
{
//在屏幕上清除
sprite->removeFromParent();
}
//在内存中销毁
world->DestroyBody(body);
}
}
} void HelloWorld::BeginContact(b2Contact *contact){
//碰撞检测,有一个为鸟游戏就结束
if (contact->GetFixtureA()->GetBody()->GetUserData()==bird || contact->GetFixtureB()->GetBody()->GetUserData()==bird)
{
//游戏结束,弹出dialog
stopGame();
CCMessageBox("Game Over", "Alert");
}
} void HelloWorld::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
//触碰屏幕时,小鸟向上运动,重力加速度:纵向 5,横向 0
bird->getB2Body()->SetLinearVelocity(b2Vec2(, 5.0f));
} void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit();
#endif
#endif
}
HelloWorldScene.h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__ #include "cocos2d.h"
#include "Box2D\Box2D.h"
#include "B2Sprite.h" #define RATIO 72.0f class HelloWorld : public cocos2d::CCLayer,public b2ContactListener
{
public:
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init(); // there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::CCScene* scene(); // a selector callback
void menuCloseCallback(CCObject* pSender); // implement the "static node()" method manually
CREATE_FUNC(HelloWorld); virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent);
virtual void BeginContact(b2Contact* contact); virtual void update(float dt); b2World *world;
B2Sprite *bird; CCSize screenSize; CCSprite *barContainer; private:
void addBird();
void addGround();
void initWorld();
void addBar(float dt);
void addBarContainer();
void startGame(float dt);
void stopGame();
}; #endif // __HELLOWORLD_SCENE_H__
Cocos2d-x FlappyBird的更多相关文章
- Cocos2d-x 2.3.3版本 FlappyBird
Cocos2d-x 2.3.3版本 FlappyBird 本篇博客基于Cocos2d-x 2.3.3, 介绍怎样开发一款之前非常火的一款游戏FlappyBird.本篇博客内容大纲例如以下: 1 ...
- 小尝试一下 cocos2d
好奇 cocos2d 到底是怎样一个框架,正好有个项目需要一个游戏框架,所以稍微了解了一下.小结一下了解到的情况. 基本概念 首先呢,因为 cocos2d 是基于 pyglet 做的,你完全可以直接用 ...
- 采用cocos2d-x lua 制作数字滚动效果样例
require "Cocos2d"require "Cocos2dConstants"local testscene = class("testsce ...
- Cocos2d 利用继承Draw方法制作可显示三维数据(宠物三维等)的三角形显示面板
很久没有写博客了,这段时间比较忙,又是搬家又是做自己的项目,还有太多琐碎的事情缠身,好不容易抽出时间把最近自己做的一些简单例子记录一下. 在我的项目中,我需要一个显示面板来显示游戏中的一个三维数据,例 ...
- cocos2dx 实现flappybird
前两天在博客园看到网友实现的一个网页版的flappy bird,挂在360游戏平台,玩了一会儿得分超低,就很想自己做一个.刚好这两天炫舞的活都清了,就弄一下玩玩. 效果图 布局类GameScene.h ...
- [Canvas前端游戏开发]——FlappyBird详解
一直想自己做点小东西,直到最近看了本<HTML5游戏开发>,才了解游戏开发中的一点点入门知识. 本篇就针对学习的几个样例,自己动手实践,做了个FlappyBird,源码共享在度盘 :也可以 ...
- iPhone开发与cocos2d 经验谈
转CSDN jilongliang : 首先,对于一个完全没有mac开发经验,甚至从没摸过苹果系统的开发人员来说,首先就是要熟悉apple的那一套开发框架(含开发环境IDE.开发框架uikit,还有开 ...
- cocos2d学习记录
视频 - http://www.manew.com/forum-105-3.html一个论坛帖 - http://www.zhihu.com/question/21114802官网 - http:// ...
- Android下Cocos2d创建HelloWorld工程
最近在搭建Cocos2d的环境,结果各种问题,两人弄了一天才能搞好一个环境-! -_-!! 避免大家也可能会遇到我这种情况,所以写一个随笔,让大家也了解下如何搭建吧- 1.环境安装准备 下载 tadp ...
- 学生信息管理系统(cocos2d引擎)——数据结构课程设计
老师手把手教了两天半,看了一下模式,加了几个功能就大功告成了!!! 给我的感想就是全都是指针! 添加图片精灵: CCSprite* spBG = CCSprite::create("&qu ...
随机推荐
- 271. Encode and Decode Strings
题目: Design an algorithm to encode a list of strings to a string. The encoded string is then sent ove ...
- Linux系统文件权限&目录权限
linux系统一切都是文件,文件和目录的所属与权限--来分别规定所有者.所有组.其余人的读.写.执行权限. 读(read),写(write),执行(excute)简写为(r.w.x),也可以以用(4. ...
- Android 去掉title bar的3个方法
1. Java代码实现 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstance ...
- sublime3配置Quick-X+自动错误提示
sublime3配置 安装Package Control 配置Quick-x API提示 配置Lua自动语法错误提示 sublime3 安装 Package Control View->Show ...
- Residual Networks <2015 ICCV, ImageNet 图像分类Top1>
本文介绍一下2015 ImageNet中分类任务的冠军——MSRA何凯明团队的Residual Networks.实际上,MSRA是今年Imagenet的大赢家,不单在分类任务,MSRA还用resid ...
- JS方法
1.方法可作为对象使用 function aa() { this.a = "aaaa"; this.b = 23; this.f = function () { alert(thi ...
- Lucene学习笔记(更新)
1.Lucene学习笔记 http://www.cnblogs.com/hanganglin/articles/3453415.html
- ERP
企业资源计划即 ERP (Enterprise Resource Planning),由美国 Gartner Group 公司于1990年提出.企业资源计划是 MRP II(企业制造资源计划)下一代的 ...
- AOJ-2249 Road Construction(最短路)
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=45523 有一个国王想在首都与各个城市之间修建公路,但是他的预算太高,所以必须 ...
- ACdream 1735 输油管道 (排序)
http://acdream.info/problem?pid=1735 官方题解:http://acdream.info/topic?tid=4246 因为主干线是平行于x轴的直线,那么跟x坐标其实 ...