[Cocos2dx-3.0beta-making flappybird] follow the trend of the times, about engine upgrade

Source: Internet
Author: User

Cocos2dx3.1 has been released for a long time. I wanted to upgrade it to the official version 3.0. However, the official version of cocos2dx3.1 does not work, and even the frame animation is out of order, give up decisively. With the support of the masses of attackers, I upgraded the code to the latest cocos2dx3.1 available. Although cocos2dx3.1 supports 3d, I don't have any 3d models here, or I can add them for fun.

Many people have also tried to upgrade directly, but the original code is correct, and I found these problems during the upgrade process:

1. A syntax error occurs first:

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

The second sentence here shows a syntax error. This is because the engine replaced the collision detection interface with a parameter from the original two parameters, and the natural syntax encountered an error, the modified code is as follows:

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

The same onContactBegin function also needs to be modified:

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

2. The second problem is that many people have discovered that the rotation angle of the bird is not normal. You only need to add a minus sign to correct the problem:

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

3. the third problem is that the collision detection fails. This is actually very simple. The previous cocos2d collision system integration is not perfect, and all kinds of masks have been set. Use the default one, but now you need to set it as follows:

First, define three categories of physical properties in the game:

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

Set to bird, ground, and pipe:

// Add the birdthis->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 groundthis->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);

Basically, with the above three changes, the program will be as cool as before. Of course, there are still some small warnings. For example, if the Object is no longer recommended, you can replace it with Ref, I have already completed the change for everyone.

V3.1 code see https://github.com/OiteBoys/Earlybird/tree/v3.1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.