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.
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 value:
- (void)onEnter{ [super onEnter]; self.userInteractionEnabled = YES; [_motionManager startAccelerometerUpdates]; } - (void)onExit{ [super onExit]; self.userInteractionEnabled = NO; [_motionManager stopAccelerometerUpdates...