cocos2d-x 《Flappy Bird 》一、滚动背景的实现

《flappy bird》是由来自越南的独立游戏开发者Dong Nguyen所开发的作品,游戏中玩家必须控制一只小鸟,跨越由各种不同长度水管所组成的障碍,而这只鸟其实是根本不会飞的……所以玩家每点击一下小鸟就会飞高一点,不点击就会下降,玩家必须控制节奏,拿捏点击屏幕的时间点,让小鸟能在落下的瞬间跳起来,恰好能够通过狭窄的水管缝隙,只要稍一分神,马上就会失败阵亡。

图片资源提取后使用PS5 处理,Zwoptex 重新打包。

======================================================================================

开发环境:

  Cocos2d-x  cocos2d-x-2.2.1

  MacBook Pro 13‘  10.9

  Xcode 5.0.2

  iPhone5   IOS 7.0.4

======================================================================================

一、效果图

cocos2d-x 《Flappy Bird 》一、滚动背景的实现

二、GameLayer层 头文件

//
//  GameLayer.h
//  Flappy
//
//  Created by yuzai on 14-2-12.
//
//

#ifndef __Flappy__GameLayer__
#define __Flappy__GameLayer__

#include "cocos2d.h"
#include "SimpleAudioEngine.h"
using namespace cocos2d;
class GameLayer:public cocos2d::CCLayer
{
public:
	GameLayer(void);
	~GameLayer(void);
    CCSprite *bg;
    CCSprite *land1;
    CCSprite *land2;
    void setBackgroud(unsigned int bg);
	CREATE_FUNC(GameLayer);
	virtual bool init();
    virtual void update(float time);
 
private:
    
    
    
};

#endif /* defined(__Flappy__GameLayer__) */

三、GameLayer层实现

//
//  GameLayer.cpp
//  Flappy
//
//  Created by yuzai on 14-2-12.
//
//

#include "GameLayer.h"
GameLayer::GameLayer(void)
{
    
}

GameLayer::~GameLayer(void)
{
}
bool GameLayer::init()
{
    if (!CCLayer::init())
    {
        return false;
    }
    //屏幕宽高比 首先 适配IPHONE5
    CCSize frameSize = CCEGLView::sharedOpenGLView()->getFrameSize();
    float scales = (float) frameSize.width/ 288;
    //CCSize winSize=CCDirector::sharedDirector()->getWinSize();
    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("res/default.plist");
    CCSpriteFrame *bg1Frame=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("bg_day.png");
    bg=CCSprite::create();
    bg->setAnchorPoint(ccp(0, 0));
    bg->setDisplayFrame(bg1Frame);
    bg->setScale(scales);
    bg->setPosition(ccp(0,0));
    addChild(bg);
    
    //2.
    
    CCSpriteFrame *landFrame=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("land.png");
    land1=CCSprite::create();
    land1->setAnchorPoint(ccp(0, 0));
    land1->setDisplayFrame(landFrame);
    land1->setScale(scales);
    land1->setPosition(ccp(0, 0));
    addChild(land1,1);
    land2=CCSprite::create();
    land2->setAnchorPoint(ccp(0, 0));
    land2->setDisplayFrame(landFrame);
    land2->setScale(scales);
    land2->setPosition(ccp(frameSize.width, 0));
    addChild(land2,1);
    
    scheduleUpdate();
    return true;
}
//改变背景
void GameLayer::setBackgroud(unsigned int bg)
{
    
    switch (bg) {
        case 1:
        {
            
        }
            break;
            
        default:
            break;
    }
}

void GameLayer::update(float time){
    //land移动逻辑
    CCSize frameSize = CCEGLView::sharedOpenGLView()->getFrameSize();

    float dif=4;
    land1->setPosition(ccp(land1->getPositionX()-dif,land1->getPositionY()));
    land2->setPosition(ccp(land2->getPositionX()-dif,land2->getPositionY()));
    if (land2->getPositionX()<0) {
        float temp=land2->getPositionX()+frameSize.width;
        land1->setPosition(ccp(temp,land2->getPositionY()));
    }
    if (land1->getPositionX()<0) {
        float temp=land1->getPositionX()+frameSize.width;
        land2->setPosition(ccp(temp,land1->getPositionY()));
    }
    
}


cocos2d-x 《Flappy Bird 》一、滚动背景的实现

上一篇:android textview设置字体的行距和字间距


下一篇:Android笔记:测量控件宽高和动态设置控件宽高