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的更多相关文章

  1. Cocos2d-x 2.3.3版本 FlappyBird

    Cocos2d-x 2.3.3版本 FlappyBird   本篇博客基于Cocos2d-x 2.3.3, 介绍怎样开发一款之前非常火的一款游戏FlappyBird.本篇博客内容大纲例如以下:   1 ...

  2. 小尝试一下 cocos2d

    好奇 cocos2d 到底是怎样一个框架,正好有个项目需要一个游戏框架,所以稍微了解了一下.小结一下了解到的情况. 基本概念 首先呢,因为 cocos2d 是基于 pyglet 做的,你完全可以直接用 ...

  3. 采用cocos2d-x lua 制作数字滚动效果样例

    require "Cocos2d"require "Cocos2dConstants"local testscene = class("testsce ...

  4. Cocos2d 利用继承Draw方法制作可显示三维数据(宠物三维等)的三角形显示面板

    很久没有写博客了,这段时间比较忙,又是搬家又是做自己的项目,还有太多琐碎的事情缠身,好不容易抽出时间把最近自己做的一些简单例子记录一下. 在我的项目中,我需要一个显示面板来显示游戏中的一个三维数据,例 ...

  5. cocos2dx 实现flappybird

    前两天在博客园看到网友实现的一个网页版的flappy bird,挂在360游戏平台,玩了一会儿得分超低,就很想自己做一个.刚好这两天炫舞的活都清了,就弄一下玩玩. 效果图 布局类GameScene.h ...

  6. [Canvas前端游戏开发]——FlappyBird详解

    一直想自己做点小东西,直到最近看了本<HTML5游戏开发>,才了解游戏开发中的一点点入门知识. 本篇就针对学习的几个样例,自己动手实践,做了个FlappyBird,源码共享在度盘 :也可以 ...

  7. iPhone开发与cocos2d 经验谈

    转CSDN jilongliang : 首先,对于一个完全没有mac开发经验,甚至从没摸过苹果系统的开发人员来说,首先就是要熟悉apple的那一套开发框架(含开发环境IDE.开发框架uikit,还有开 ...

  8. cocos2d学习记录

    视频 - http://www.manew.com/forum-105-3.html一个论坛帖 - http://www.zhihu.com/question/21114802官网 - http:// ...

  9. Android下Cocos2d创建HelloWorld工程

    最近在搭建Cocos2d的环境,结果各种问题,两人弄了一天才能搞好一个环境-! -_-!! 避免大家也可能会遇到我这种情况,所以写一个随笔,让大家也了解下如何搭建吧- 1.环境安装准备 下载 tadp ...

  10. 学生信息管理系统(cocos2d引擎)——数据结构课程设计

    老师手把手教了两天半,看了一下模式,加了几个功能就大功告成了!!! 给我的感想就是全都是指针! 添加图片精灵: CCSprite*  spBG = CCSprite::create("&qu ...

随机推荐

  1. Android 闹钟设置

    在Android中可以通过AlarmManager 来实现闹钟,AlarmManager类是专门用来设定在某个指定的时间去完成指定的事件.AlarmManager 提供了访问系统警报的服务,只要在程序 ...

  2. URAL 1066 Garland 二分

    二分H2的位置,判断条件为是否有Hi < 0 #include <cstdio> #include <cstring> #include <cstdlib> ...

  3. jdbc框架 commons-dbutils的使用

    commons-dbutils 是 Apache 组织提供的一个开源 JDBC工具类库,它是对JDBC的简单封装,学习成本极低,并且使用dbutils能极大简化jdbc编码的工作量,同时也不会影响程序 ...

  4. NandFlash详述【转】

    NandFlash详述 转自:http://wenku.baidu.com/view/04d9330bb52acfc789ebc92f.html?re=view 1. 硬件特性: [Flash的硬件实 ...

  5. SQLite学习手册(内置函数)

    一.聚合函数: SQLite中支持的聚合函数在很多其他的关系型数据库中也同样支持,因此我们这里将只是给出每个聚集函数的简要说明,而不在给出更多的示例了.这里还需要进一步说明的是,对于所有聚合函数而言, ...

  6. js中substr与substring的用法与区别

    substrsubstr(start,length)表示从start位置开始,截取length长度的字符串. var src="images/pic_1.png";alert(sr ...

  7. Kernel rest_init相关

    Linux系统里,有些进程只有kernel部分的代码,即由一个kernel函数进入,在sched的时候,将其与用户进程同等对待. PID为0的叫swapper或sched进程,对应函数为rest_in ...

  8. iPhone(iOS设备) 无法更新或恢复时, 如何进入恢复模式

    在更新或恢复 iPhone  时,如果遇到以下所列问题之一.可能就要将设备置于恢复模式,并尝试重新恢复设备. 设备不断地重新启动,但从未显示主屏幕. 无法完成更新或恢复,且 iTunes 不再能识别设 ...

  9. 第十篇 PO核心功能及流程详解

    详见链接:http://bbs.erp100.com/thread-272866-1-1.html1. P2P lifecycleP2P是procure to pay的缩写,p2p循环值得就是采购到付 ...

  10. ural1238. Folding(记忆化)

    1238 这算模拟加记忆化吗 找bug找了2个多小时..记忆化部分好想 就是字符串处理部分挫了 一个个复制模拟 各种修改查找 #include <iostream> #include< ...