O'Reilly logo

Cocos2d-x Cookbook by Akihiro Matsuura

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using joints

Joints are used to connect two physics bodies to each other. Then, you can create a complex shape to join some shapes. In addition, you can create objects such as a gear or a motor to use joints. Cocos2d-x has many different types of joints. In this recipe, we explain a typical joint type.

Getting ready

You will create a method that creates a physics object. That's why you have to create multiple physics objects. This method is called makeSprite. You have to add the following code in HelloWorld.h:

cocos2d::Sprite* makeSprite();

You have to add the following code in HelloWorld.cpp:

Sprite* HelloWorld::makeSprite() { auto sprite = Sprite::create("CloseNormal.png"); auto physicsBody = PhysicsBody::createCircle(sprite->getContentSize().width/2); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required