【cocos2dx-3.0beta-制作flappybird】跟随时代的潮流,关于引擎升级

杨阳飇
2023-12-01

cocos2dx3.1版本早就已经发布出来了,本来我本人想把这个升级到正式的3.0版本,不过无奈3.0的正式版坑的不行,连帧动画都妥妥的出问题,果断放弃,随着广大淫民的支持,我将代码升级为最新的cocos2dx3.1可用,虽然cocos2dx3.1支持3d不过我这里没什么3d模型,不然也可以加进去玩玩。

很多人也去试过直接升级,不过原有的代码妥妥的出现了问题,我在升级的过程当初也发现了这些问题:

1.首先出现的一个问题就是一个语法错误:

auto contactListener = EventListenerPhysicsContact::create();
contactListener->onContactBegin = CC_CALLBACK_2(GameLayer::onContactBegin, this);
this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this);

这里的第二句出现了语法错误,这个是因为引擎把碰撞检测的接口换掉了由原来的两个参数换成了一个参数,自然语法上就出现了错误,下面是修改后的代码:

auto contactListener = EventListenerPhysicsContact::create();
contactListener->onContactBegin = CC_CALLBACK_1(GameLayer::onContactBegin, this);
this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this);

同样函数onContactBegin也需要做同样的修改:

bool GameLayer::onContactBegin(PhysicsContact& contact) {
	this->gameOver();
	return true;
}

2.第二个问题其实很多人已经发现了,就是小鸟的旋转角度不正常,这里只需要加个减号进行修正就可以了:

void GameLayer::rotateBird() {
    float verticalSpeed = this->bird->getPhysicsBody()->getVelocity().y;
    this->bird->setRotation(- min(max(-90, (verticalSpeed*0.2 + 60)), 30));
}

3.第三个问题是碰撞检测不行了,这个其实也很简单,之前的cocos2d的碰撞系统集成并不是很完美,各类掩码都已经被设定好了,用默认的就行,但是现在需要自己设定了,修改如下:

首先定义游戏里面的三类拥有物理属性的东东:

typedef enum : uint8_t {
    ColliderTypeBird        = 0x1,
    ColliderTypeLand        = 0x1<<1,
    ColliderTypePip         = 0x1<<2
} ColliderType;

设定到鸟,地面和管子上面:

// Add the bird
this->bird = BirdSprite::getInstance();
this->bird->createBird();
PhysicsBody *body = PhysicsBody::create();
body->addShape(PhysicsShapeCircle::create(BIRD_RADIUS));
body->setCategoryBitmask(ColliderTypeBird);
body->setCollisionBitmask(ColliderTypeLand & ColliderTypePip);
body->setContactTestBitmask(ColliderTypePip);
body->setDynamic(true);
body->setLinearDamping(0.0f);
body->setGravityEnable(false);
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);

...
// Add the ground
this->groundNode = Node::create();
float landHeight = BackgroundLayer::getLandHeight();
auto groundBody = PhysicsBody::create();
groundBody->addShape(PhysicsShapeBox::create(Size(288, landHeight)));
groundBody->setDynamic(false);
groundBody->setLinearDamping(0.0f);
groundBody->setCategoryBitmask(ColliderTypeLand);
groundBody->setCollisionBitmask(ColliderTypeBird);
groundBody->setContactTestBitmask(ColliderTypeBird);
this->groundNode->setPhysicsBody(groundBody);
this->groundNode->setPosition(144, landHeight/2);
this->addChild(this->groundNode); 

...
body->addShape(shapeBoxDown);
body->addShape(PhysicsShapeBox::create(pipUp->getContentSize()));
body->setDynamic(false);
body->setCategoryBitmask(ColliderTypePip);
body->setCollisionBitmask(ColliderTypeBird);
body->setContactTestBitmask(ColliderTypeBird);
singlePip->setPhysicsBody(body);

基本上就以上三点修改,程序就能够和以前一样生龙活虎了,当然还有些小的warning,比如Object已经推荐不使用了,大家换成Ref就行了,我已经替大家改完了。

v3.1的代码见https://github.com/OiteBoys/Earlybird/tree/v3.1

 类似资料: