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

Applying force with the accelerometer

The difference between an impulse and force is that the impulse is just applied to the body once, whereas a force is applied over a period of time. Let's take a look at how to apply force to the same object.

Getting started

In the MainScene.h file, we will import a core motion header and create a new motion manager of the CMMotionManager type, as follows:

#import "cocos2d.h"
#import <CoreMotion/CoreMotion.h>

@interface MainScene : CCNode <CCPhysicsCollisionDelegate>{

  CGSize winSize;
  CGPoint center;

  CCPhysicsNode *_physicsWorld;

  CCSprite *basketSprite;

  CMMotionManager *_motionManager;
}

+(CCScene*)scene;

@end

In the onEnter and onExit functions, we will add the following lines of code to update the accelerometer ...

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