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 impulse with the touch control

We can also apply impulse and force to the objects in a particular direction. This makes creating a jump very easy in games.

Getting started

We will apply force to the basket once the screen is tapped, so we need to enable touch in the scene.

So, enable it in the scene as follows:

- (void)onEnter{
  [super onEnter];
  self.userInteractionEnabled = YES;

}

- (void)onExit{
  [super onExit];
  self.userInteractionEnabled = NO;

}

How to do it…

We will create a touchBegan function, as follows:

- (void)touchBegan:(CCTouch *)touch withEvent:(UIEvent *)event { // we want to know the location of our touch in this scene CGPoint touchLocation = [touch locationInNode:self]; if(touchLocation.x < winSize.width/2){ [basketSprite.physicsBody ...

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