近期才開始学cocos2dx,买了几本书还有看大神(主要是 笨木头)的博客。然后就自己尝试用cocos2d-x实现了一下...

(新手,勿喷...)

先看执行效果 http://pan.baidu.com/s/1hqBsCYS

转载请注明出处:http://blog.csdn.net/joueu

你看那仅仅笨鸟好逼真啊(事实上是叫FlappyRect啦)

TollgateScene.cpp

//
// TollgateScene.cpp
// FlappyRect
//
// Created by Joueu on 14-10-7.
//
// #include "TollgateScene.h"
#include "Pipe.h"
#include "shareHeader.h"
#include "MenuScene.h" bool TollgateScene::init()
{
if (!Layer::init()) {
return false;
} createOprUI(); Size visibleSize = Director::getInstance() -> getVisibleSize();
Point visiblePoint = Director::getInstance() -> getVisibleOrigin();
m_bird = Bird::create();
m_bird -> setPosition(visibleSize.width * 0.2f, visibleSize.height * 0.5f);
this -> addChild(m_bird); m_pipesLayer = PipesLayer ::create();
m_pipesLayer -> setPosition(0, 170);
this -> addChild(m_pipesLayer); schedule(schedule_selector(TollgateScene::logic)); // auto contactListener = EventListenerPhysicsContact::create();
// contactListener -> onContactBegin = CC_CALLBACK_1(TollgateScene::onContactBegin, this);
// _eventDispatcher -> addEventListenerWithSceneGraphPriority(contactListener, this); auto contactListener = EventListenerPhysicsContact::create();
contactListener -> onContactBegin = CC_CALLBACK_1(TollgateScene::onContactBegin, this);
_eventDispatcher -> addEventListenerWithSceneGraphPriority(contactListener, this); return true;
} void TollgateScene::createOprUI()
{
Node *UI = timeline::NodeReader::getInstance() -> loadNodeWithFile("Flappy_1.ExportJson");
m_flyBtn = (Button *)Helper::seekWidgetByName((Widget *)UI, "flyBtn");
m_flyBtn -> addTouchEventListener(this, toucheventselector(TollgateScene::flyBtnPressed)); Size btnSize = Size(640,210);
PhysicsBody *body = PhysicsBody::createBox(btnSize);
body -> setDynamic(false);
body -> setCollisionBitmask(1);
body -> setCategoryBitmask(1);
body -> setContactTestBitmask(1);
m_flyBtn -> setPhysicsBody(body); // m_bg1 = (Sprite *)Helper::seekWidgetByName((Widget *)UI, "borderImg"); m_bg1 =(Sprite *) UI -> getChildren().at(2);
m_bg2 = Sprite::create("border.png");
m_bg2 -> setPosition(960,m_bg1 -> getPositionY()); this -> addChild(m_bg2);
this -> addChild(UI);
} void TollgateScene::flyBtnPressed(Ref *sender,TouchEventType type)
{
switch (type) {
case TouchEventType::TOUCH_EVENT_ENDED:
m_bird -> jump();
break; default:
break;
}
} Scene *TollgateScene::scene()
{ Scene *scene = Scene::createWithPhysics(); Vect gravity(0,-500.0f); scene -> getPhysicsWorld() -> setGravity(gravity); TollgateScene *layer = TollgateScene::create(); scene -> addChild(layer); return scene;
} void TollgateScene::logic(float dt)
{
int PosX1 = m_bg1 -> getPositionX();
int PosX2 = m_bg2 -> getPositionX(); int iSpeed = 4; PosX1 -= iSpeed;
PosX2 -= iSpeed; int visibleWidth = Director::getInstance() -> getVisibleSize().width; if (PosX1 < - 0.5f * visibleWidth) {
PosX2 = 0.5f * visibleWidth;
PosX1 = 1.5f * visibleWidth;
}
if (PosX2 < -0.5f * visibleWidth) {
PosX1 = 0.5f * visibleWidth;
PosX2 = 1.5f * visibleWidth;
} m_bg1 -> setPositionX(PosX1);
m_bg2 -> setPositionX(PosX2);
} TollgateScene::~TollgateScene()
{ } bool TollgateScene::onContactBegin(PhysicsContact &contact)
{
auto nodeA =(Sprite *) contact.getShapeA() -> getBody() -> getNode();
auto nodeB = (Sprite *)contact.getShapeB() -> getBody() -> getNode(); if (nodeA == NULL && nodeB == NULL) {
return false;
} if ((nodeA -> getTag() == shareHeader_BirdTag && nodeB -> getTag() == shareHeader_PipeTag) ||
(nodeA -> getTag() == shareHeader_PipeTag && nodeB -> getTag() == shareHeader_BirdTag)) {
Director::getInstance() -> replaceScene(TransitionSlideInT::create(1.0f,MenuScene::scene()));
}
return true;
} void TollgateScene::onExit()
{
Layer :: onExit();
_eventDispatcher -> removeEventListenersForTarget(this);
}

TollgateScene.h

//
// TollgateScene.h
// FlappyRect
//
// Created by Joueu on 14-10-7.
//
// #ifndef __FlappyRect__TollgateScene__
#define __FlappyRect__TollgateScene__ #include "cocos2d.h"
#include "cocostudio/Cocostudio.h"
#include "editor-support/cocostudio/CCSGUIReader.h"
#include "ui/CocosGUI.h"
#include "Bird.h"
#include "PipesLayer.h" using namespace cocos2d::ui;
using namespace cocostudio;
USING_NS_CC; class TollgateScene : public Layer
{
public:
CREATE_FUNC(TollgateScene); virtual bool init(); static Scene *scene(); ~TollgateScene();
virtual void onExit() override;
private:
void flyBtnPressed(Ref *sender,TouchEventType type);
void createOprUI();
Button *m_flyBtn;
Bird *m_bird;
void logic(float dt); Sprite *m_bg1;
Sprite *m_bg2; PipesLayer *m_pipesLayer; bool onContactBegin (PhysicsContact &contact);
}; #endif /* defined(__FlappyRect__TollgateScene__) */

PipesLayer.cpp

//
// PipesLayer.cpp
// FlappyRect
//
// Created by Joueu on 14-10-7.
//
// #include "PipesLayer.h" PipesLayer::PipesLayer()
{ } PipesLayer::~PipesLayer()
{ } bool PipesLayer::init()
{
// srand(time(0));
m_TimeCount = 0;
schedule(schedule_selector(PipesLayer::logic));
return true;
} int gameRand(int vMin,int vMax){
return (2.0f*random() / 0xFFFFFFFFu) * ((vMax+1)-vMin) + vMin;
} void PipesLayer::logic(float dt)
{
Size visibleSize = Director::getInstance() -> getVisibleSize();
m_TimeCount += dt;
if (m_TimeCount > 4)
{
// log("%d",rand() % 700);
// int randomPosition = rand() % 700;
int randomPosition = gameRand(80, 600); m_TimeCount -= 4;
Pipe *pipe = Pipe::create(randomPosition);
pipe -> setPosition(Vec2(visibleSize.width * 1.5f, this -> getContentSize().height * 0.5f));
this -> addChild(pipe);
m_vector.pushBack(pipe);
} for (Pipe *p : m_vector){
p -> setPositionX(p -> getPositionX() - shareHeader_speed);
if (p -> getPositionX() < -visibleSize.width * 0.5) {
m_vector.eraseObject(p);
// p -> getPhysicsBody() -> removeFromWorld();
p -> removeFromParent();
}
}
}

PipesLayer.h

//
// PipesLayer.h
// FlappyRect
//
// Created by Joueu on 14-10-7.
//
// #ifndef __FlappyRect__PipesLayer__
#define __FlappyRect__PipesLayer__ #include "cocos2d.h"
#include "Pipe.h"
#include "shareHeader.h"
USING_NS_CC; class PipesLayer:public Node
{
public:
PipesLayer();
~PipesLayer();
virtual bool init();
CREATE_FUNC(PipesLayer);
void logic(float dt); private:
float m_TimeCount;
Vector<Pipe *>m_vector;
}; #endif /* defined(__FlappyRect__PipesLayer__) */

Brid.cpp

//
// Bird.cpp
// FlappyRect
//
// Created by Joueu on 14-10-7.
//
// #include "Bird.h"
#include "shareHeader.h"
Bird::Bird()
{ } Bird::~Bird()
{ } bool Bird::init()
{ if (!Layer::init()) {
return false;
}
this -> setTag(shareHeader_BirdTag);
Sprite *sprite = Sprite::create("rect.jpg"); Size spriteSize = sprite -> getContentSize(); this -> setContentSize(spriteSize); PhysicsBody *body = PhysicsBody::createBox(spriteSize); // body -> setDynamic(false);
// body -> setRotationEnable(false);
body -> setCategoryBitmask(1);
body -> setCollisionBitmask(1);
body -> setContactTestBitmask(1); this -> setPhysicsBody(body);
this -> addChild(sprite); return true;
} void Bird::jump()
{
// this -> getPhysicsBody() -> applyImpulse(Vect(0,100000));
this -> getPhysicsBody() -> setVelocity(Vect(0,300));
}

Brid.h

//
// Bird.h
// FlappyRect
//
// Created by Joueu on 14-10-7.
//
// #ifndef __FlappyRect__Bird__
#define __FlappyRect__Bird__ #include "cocos2d.h" USING_NS_CC; class Bird : public Layer
{
public:
Bird();
~Bird();
CREATE_FUNC(Bird);
virtual bool init();
void jump();
}; #endif /* defined(__FlappyRect__Bird__) */

Pipe.cpp

<pre name="code" class="cpp">//
// Pipe.cpp
// FlappyRect
//
// Created by Joueu on 14-10-7.
//
// #include "Pipe.h"
#include "shareHeader.h" bool Pipe::init(int randomPosition)
{
m_lowerPipe = Sprite::create("pipe.png");
float pipeHeight = m_lowerPipe -> getContentSize().height;
float pipeWidth = m_lowerPipe -> getContentSize().width;
m_lowerPipe -> setScaleY( (randomPosition) / pipeHeight);
m_lowerPipe -> setPosition(Vec2(pipeWidth * 0.5f, pipeHeight * 0.5f *m_lowerPipe -> getScaleY()));
this -> setContentSize(Size(pipeWidth,shareHeader_gameSceneHeight)); m_lowerPipe -> setTag(shareHeader_PipeTag); m_upperPipe = Sprite::create("pipe.png");
m_upperPipe -> setScaleY((shareHeader_gameSceneHeight - (m_lowerPipe -> getScaleY() * pipeHeight) - shareHeader_emptyHeight)/ pipeHeight);
m_upperPipe -> setPosition(Vec2(pipeWidth * 0.5f,shareHeader_gameSceneHeight - pipeHeight * m_upperPipe -> getScaleY() * 0.5));
m_upperPipe -> setFlippedY(true);
m_upperPipe -> setTag(shareHeader_PipeTag); PhysicsBody *lowerbody = PhysicsBody::createBox(Size(pipeWidth,pipeHeight *m_lowerPipe -> getScaleY()));
lowerbody -> setDynamic(false);
lowerbody -> setCategoryBitmask(1);
lowerbody -> setCollisionBitmask(1);
lowerbody -> setContactTestBitmask(1);
m_lowerPipe -> setPhysicsBody(lowerbody); PhysicsBody *upperbody = PhysicsBody::createBox(Size(pipeWidth,pipeHeight *m_upperPipe -> getScaleY()));
upperbody -> setDynamic(false);
upperbody -> setCategoryBitmask(1);
upperbody -> setCollisionBitmask(1);
upperbody -> setContactTestBitmask(1);
m_upperPipe -> setPhysicsBody(upperbody); this -> addChild(m_upperPipe);
this -> addChild(m_lowerPipe);
return true;
} Pipe *Pipe::create(int randomPosition)
{
auto pipe = new Pipe();
if (pipe && pipe -> init(randomPosition)) {
pipe -> autorelease();
}else
{
CC_SAFE_DELETE(pipe);
}
return pipe;
} Pipe::~Pipe()
{
}

Pipe.h

//
// Pipe.h
// FlappyRect
//
// Created by Joueu on 14-10-7.
//
// #ifndef __FlappyRect__Pipe__
#define __FlappyRect__Pipe__ #include "cocos2d.h" USING_NS_CC; class Pipe: public Node
{
public:
static Pipe*create(int randomPosition);
bool init(int randomPosition);
~Pipe();
private:
Sprite *m_upperPipe;
Sprite *m_lowerPipe;
}; #endif /* defined(__FlappyRect__Pipe__) */

MenuScene.cpp

//
// MenuScene.cpp
// FlappyRect
//
// Created by Joueu on 14-10-7.
//
// #include "MenuScene.h"
#include "TollgateScene.h" bool MenuScene::init()
{
auto visibleSize = Director :: getInstance() -> getVisibleSize();
auto labelStart = Label::create("Start Game", "Monaco", 40);
MenuItemLabel *label = MenuItemLabel::create(labelStart,CC_CALLBACK_1(MenuScene::labelPressed, this));
auto menu = Menu::create(label, NULL);
this -> addChild(menu);
return true;
} Scene *MenuScene::scene()
{
Scene *scene = Scene::create(); MenuScene *layer = MenuScene::create(); scene -> addChild(layer); return scene;
} void MenuScene::labelPressed(Ref *sender)
{
Director::getInstance() -> replaceScene(TransitionFade::create(1.0f, TollgateScene::scene()));
}

MenuScene.h

//
// MenuScene.h
// FlappyRect
//
// Created by Joueu on 14-10-7.
//
// #ifndef __FlappyRect__MenuScene__
#define __FlappyRect__MenuScene__ #include "cocos2d.h" USING_NS_CC; class MenuScene :public Layer
{
public:
CREATE_FUNC(MenuScene);
virtual bool init();
static Scene *scene();
private:
void labelPressed(Ref *sender);
}; #endif /* defined(__FlappyRect__MenuScene__) */

shareHeader.h

//
// shareHeader.h
// FlappyRect
//
// Created by Joueu on 14-10-7.
//
// #ifndef FlappyRect_shareHeader_h
#define FlappyRect_shareHeader_h #define shareHeader_bottomHeight 170
#define shareHeader_gameSceneHeight 960
#define shareHeader_emptyHeight 250
#define shareHeader_speed 3
#define shareHeader_BirdTag 1001
#define shareHeader_PipeTag 1002 #endif

AppDelegate.cpp

#include "AppDelegate.h"
#include "HelloWorldScene.h"
#include "TollgateScene.h"
#include "MenuScene.h"
USING_NS_CC; AppDelegate::AppDelegate() { } AppDelegate::~AppDelegate()
{
} bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
if(!glview) {
glview = GLView::create("My Game");
director->setOpenGLView(glview);
} glview -> setDesignResolutionSize(640, 1136, ResolutionPolicy::EXACT_FIT);
// turn on display FPS
director->setDisplayStats(true); // set FPS. the default value is 1.0/60 if you don't call this
director->setAnimationInterval(1.0 / 60); // create a scene. it's an autorelease object
// auto scene = HelloWorld::createScene();
auto scene = MenuScene::scene(); // run
director->runWithScene(scene); return true;
} // This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground() {
Director::getInstance()->stopAnimation(); // if you use SimpleAudioEngine, it must be pause
// SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
} // this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
Director::getInstance()->startAnimation(); // if you use SimpleAudioEngine, it must resume here
// SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}

因为写得比較仓促,就没有写凝视了,不要介意哈...

图片资源在这里:图片资源

用cocos2d-x 3.2 实现的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. 深入浅出Hadoop实战开发(HDFS实战图片、MapReduce、HBase实战微博、Hive应用)

    Hadoop是什么,为什么要学习Hadoop?     Hadoop是一个分布式系统基础架构,由Apache基金会开发.用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群的威力高速运 ...

  2. hdu4722 Good Numbers

    Good Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...

  3. VB与报表的交互

    还接着上次说.在上个博客中已经说到建立好表的步骤了,接下来就是怎么使表与vb连接. 先看一下代码. Option Explicit Dim WithEvents Report As grproLibC ...

  4. Re-installation failed due to different application signatures.

    出现此问题是由于apk的签名不同所致(假设不知道签名是什么  请看上一篇Android应用程序签名 debug签名).假设你是使用的自己的签名,那就是你新版本号的apk使用的签名文件与上一版本号(也就 ...

  5. 5.单行函数,多行函数,字符函数,数字函数,日期函数,数据类型转换,数字和字符串转换,通用函数(case和decode)

     1  多行函数(理解:有多个输入,但仅仅输出1个结果) SQL>select count(*) from emp; COUNT(*) ------------- 14 B 字符函数Lowe ...

  6. Cocos2dx中Plugin-X 在android下的整合

    直接拉plugin-x中的jar包导入到Eclipse中就可以.用这么麻烦的工具干嘛.

  7. 经常使用MD5算法代码

    经常使用的MD5算法代码日期: 2014年8月4日作者: 铁锚 MD5,全称为 Message Digest Algorithm 5(消息摘要算法第五版).详情请參考 维基百科:MD5  MD5加密后 ...

  8. Find the minimum线段树成段更新

    问题 G: Find the minimum 时间限制: 2 Sec   内存限制: 128 MB 提交: 83   解决: 20 [ 提交][ 状态][ 讨论版] 题目描述 Given an int ...

  9. 链栈之C++实现

    链栈是借用单链表实现的栈.其不同于顺序栈之处在于: 1.链栈的空间是程序运行期间根据需要动态分配的,机器内存是它的上限.而顺序栈则是 静态分配内存的. 2.链栈动态分配内存的特性使得它一般无需考虑栈溢 ...

  10. Mysql自增主键ID重新排序方法详解

    Mysql数据库表的自增主键ID号乱了,需要重新排列. 原理:删除原有的自增ID,重新建立新的自增ID. 1,删除原有主键: ALTER TABLE `table_name` DROP `id`; 2 ...