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

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

一、效果图




二、绘制碰撞框(添加以下代码)

void CCSprite::draw(void)
{
   
    // 2014年02月20日15:53:35
    // draw bounding box
//#if kDebugDrawNode == 1
    // draw bounding box
    CCPoint pt;
    pt.x = 0;
    pt.y = 0;
    CCSize size = boundingBox().size;
    CCPoint vertices[4]={
        ccp(pt.x, pt.y + size.height),
        ccp(pt.x, pt.y),
        ccp(pt.x + size.width, pt.y),
        ccp(pt.x + size.width, pt.y + size.height)
    };
    
    ccDrawPoly(vertices, 4, true);
//#endif // CC_SPRITE_DEBUG_DRAW
    
    CC_INCREMENT_GL_DRAWS(1);
}

三、碰撞检测

void GameLayer::birdCrash()
{
    //监测碰撞
    CCObject *pi;
    //
    //屏幕宽高比 首先 适配IPHONE5
    CCSize frameSize = CCEGLView::sharedOpenGLView()->getFrameSize();
    //float scales = (float) frameSize.width/ 288;
    float scales=1.0f;
    CCRect birdRect=bird->boundingBox();
    float x=birdRect.origin.x+birdRect.size.width*scales;
    float y=birdRect.origin.y+birdRect.size.height*scales;
    float width=birdRect.size.width*scales;
    float height=birdRect.size.height*scales;
    CCRect crash_rect=CCRectMake(x, y, width,height);
    CCRect crasj=CCRectMake(birdRect.origin.x, birdRect.origin.y,birdRect.size.width,birdRect.size.height);
    
    //陆地
    CCRect landRect=CCRectMake(land1->boundingBox().origin.x,land1->boundingBox().origin.y,frameSize.width,land1->boundingBox().size.height);
    bool isCrash=false;
    CCARRAY_FOREACH(pipeArray,pi)
    {
        CCSprite *p=(CCSprite*)pi;
        if (p->boundingBox().intersectsRect(birdRect)) {
            isCrash=true;
            break;
        }
        
        
    }

    if (landRect.intersectsRect(birdRect)) {
        isCrash=true;
    }
    if (isCrash) {
        SimpleAudioEngine::sharedEngine()->playEffect("res/sfx_hit.caf");
        
        //停止游戏动作
        
        //静止触摸
        setTouchEnabled(false);
        
        //bird->stopAllActions();
        //bird->body->stopAllActions();
        //p->stopAllActions();
        bird->m_pBody->SetAwake(true);
        //停止陆地运动
        land1->stopAllActions();
        land2->stopAllActions();
        //停止水管运动
        CCObject *pipeObject;
        CCARRAY_FOREACH(pipeArray,pipeObject)
        {
            
            CCSprite *pipe=(CCSprite*)pipeObject;
            pipe->stopAllActions();
        }
        //状态机
        GAME_STATE=GAME_OVER;

    }
    
    
    
}

源代码:http://pan.baidu.com/s/1eQkRru6

软件达到基本效果 碰撞加分 等等并未加入

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值