O'Reilly logo

Cocos2d Cross-Platform Game Development Cookbook - Second Edition by Siddharth Shekar

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

Adding motor joints

Until now, we applied a linear force to the basket to make it move, and the wheels rotated accordingly. With a motor joint, we can rotate the wheels, and the body of the object will behave accordingly.

Getting started

In the MainScene.h file, create two global variables of the CCPhysicsJoint type and call them motorL and motorR. Then, run the following code:

    CCPhysicsNode *_physicsWorld;
    CCSprite *basketSprite;
    CMMotionManager *_motionManager;
    CCSprite *wheelSpriteL, *wheelSpriteR;

    CCPhysicsJoint *motorL;
    CCPhysicsJoint *motorR;

How to do it…

We will replace touchBegan with the following code:

- (void)touchBegan:(CCTouch *)touch withEvent:(UIEvent *)event{ // we want to know the location of our touch in this scene CGPoint touchLocation ...

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