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);
bool GameLayer::onContactBegin(PhysicsContact& contact) {
this->gameOver();
return true;
}
void GameLayer::rotateBird() {
float verticalSpeed = this->bird->getPhysicsBody()->getVelocity().y;
this->bird->setRotation(- min(max(-90, (verticalSpeed*0.2 + 60)), 30));
}
首先定义游戏里面的三类拥有物理属性的东东:
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);