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. CodeForces485B——Valuable Resources(水题)

    Valuable Resources Many computer strategy games require building cities, recruiting army, conquering ...

  2. WebMvcConfigurerAdapter

    spring Boot 默认的处理方式就已经足够了,默认情况下Spring Boot 使用WebMvcAutoConfiguration中配置的各种属性. 建议使用Spring Boot 默认处理方式 ...

  3. SGU 106 The equation 扩展欧几里得好题

    扩展欧几里得的应用……见算法竞赛入门经典p.179 注意两点:1.解不等式的时候除负数变号 2.各种特殊情况的判断( a=0 && b=0 && c=0 ) ( a=0 ...

  4. MYSQL数据库错误代码提示汇总

    Mysql出错代码表 1005:创建表失败 1006:创建数据库失败 1007:数据库已存在,创建数据库失败 1008:数据库不存在,删除数据库失败 1009:不能删除数据库文件导致删除数据库失败 1 ...

  5. 【Tech】Cassandra安装和启动

    1.安装 jre,配置系统环境变量: 2.安装python,配置环境变量: 3.下载cassandra,http://cassandra.apache.org/download/: 4.解压,这里我没 ...

  6. 用状态矩阵解决有序操作的case爆炸问题(转载)

    转自http://qa.baidu.com/blog/?p=167 作者:qabloger 一. 简介 我们在测试中可能都会面对case爆炸问题.有的case组合是无序的,我们可以通过pict[1]组 ...

  7. c++异常 连续抛出异常

      今天天遇到这样一个问题,连续两次抛出异常,但是只有一个catch,会导致core这个时候会导致core, 单线程编程中可能很少遇到这样的问题,但是多线程中是很容易遇到的, 举个例子:catch代码 ...

  8. 如何通过session控制单点登录

    web服务器为每一个浏览器实例对应一个session.这个session有自己的一个独立id,这个id保存在浏览器的cookie中(这个cookie貌似随着这个浏览器实例的关闭而清除),访问web服务 ...

  9. [HDOJ1160]FatMouse's Speed(DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160 FatMouse believes that the fatter a mouse is, th ...

  10. Codeforces Beta Round #97 (Div. 1)

    B 判矩阵的时候 出了点错 根据点积判垂直 叉积判平行 面积不能为0 #include <iostream> #include<cstdio> #include<cstr ...