【cocos2dx-3.0beta-制作flappybird】终于要来正戏了—游戏层的设计和小鸟的加入

一、引言

在上一节当中,我们分析了游戏层跟控制层的关系,而在这一节当中,我们将会详细介绍游戏层(GameLayer)的具体实现。


二、游戏层的基本结构

单游戏层来说,它包括两个元素:
1、小鸟
2、障碍物(水管)

游戏层的操作:
1、游戏状态的表示
2、添加小鸟和水管
3、分数的实时统计
4、碰撞的检测

三、游戏状态的表示

采用枚举类型表示游戏的三种状态:准备状态、游戏进行状态、游戏结束状态
/**
* Define the game status
* GAME_STATUS_READY game is not start, just ready for payer to start.
* GAME_STATUS_START the game is started, and payer is paying this game.
* GAME_STATUS_OVER the player is lose this game, the game is over.
*/
typedef enum _game_status {
GAME_STATUS_READY = 1,
GAME_STATUS_START,
GAME_STATUS_OVER
} GameStatus;

四、添加小鸟

由于前面的铺垫,小鸟类的单独封装,所以在这里,我们添加一直小鸟就变得非常简单了。
首先,在GameLayer声明了这只小鸟
BirdSprite *bird;

然后再设置这支小鸟的基本属性
this->bird->setPhysicsBody(body); 
this->bird->setPosition(origin.x + visiableSize.width*1/3 - 5,origin.y + visiableSize.height/2 + 5);
this->bird->idle();

最后添加这支小鸟到游戏层中
this->addChild(this->bird);

五、水管的添加

水管的添加,主要是通过createPips()函数来生成水管,有关水管的详细生成过程将在后续章节讨论

六、游戏分数的更新

void GameLayer::checkHit() {
    for(auto pip : this->pips) {
        if (pip->getTag() == PIP_NEW) {
            if (pip->getPositionX() < this->bird->getPositionX()) {
                SimpleAudioEngine::getInstance()->playEffect("sfx_point.ogg");
                this->score ++;
                this->delegator->onGamePlaying(this->score);
                pip->setTag(PIP_PASS);
           }
         }
    }
}



1、通过设置水管的Tag来标记水管是否已经被成功通过,初始创建的水管标记为PIP_NEW。
2、通过判断水管和小鸟的横坐标来判断是否成功通过了水管。
3、通过代理将分数传到ststuslayer,在游戏状态层实时更新分数。

七、碰撞检测

在GameLayer初始化的时候,我们创建了一个碰撞监听:
auto contactListener = EventListenerPhysicsContact::create();
contactListener->onContactBegin = CC_CALLBACK_2(GameLayer::onContactBegin, this);
this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this);

该监听的回调函数为GameLayer::onContactBegin,可见一旦出现碰撞,则进入游戏结束状态。
bool GameLayer::onContactBegin(EventCustom *event, const PhysicsContact& contact) {
this->gameOver();
return true; 
}

八、小结

本节对游戏层进行了一个详细的介绍,有关详细代码,还请移步到github: https://github.com/OiteBoys/Earlybird
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值