Ray
Ray

Reputation: 161

Apply Angular Impulse

Here is my code for the player and the ball that interact with each other. What I want to do is to apply force to the ball like if my player is shooting it. I want the ball to move away from my player with force. how can I apply Impulse or force to this. I have tried many times but I am a newbie with Sprite Kit.

- (void) Player {
    _Player = [SKSpriteNode spriteNodeWithImageNamed:@"player1"];
    _Player.xScale = 0.09;
    _Player.yScale = 0.09;
    _Player.position = CGPointMake(self.size.width/4, self.size.height/2);
    _Player.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:_Player.size];
    _Player.physicsBody.dynamic = NO;
    [self addChild:_Player];
}

- (void) TheMethodForBall {
    SKSpriteNode *sprites = [SKSpriteNode spriteNodeWithImageNamed:@"ball"];
    sprites.xScale = 0.19;
    sprites.yScale = 0.19;
    sprites.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprites.size];
    sprites.physicsBody.dynamic = YES;
    sprites.physicsBody.affectedByGravity = NO;
    sprites.physicsBody.allowsRotation = YES;
    sprites.physicsBody.restitution = YES;
    sprites.physicsBody.angularVelocity = 4;
    sprites.physicsBody.usesPreciseCollisionDetection = YES;
    [self addChild:sprites];
}

Upvotes: 9

Views: 1599

Answers (1)

DogCoffee
DogCoffee

Reputation: 19966

I think you want to apply an impulse like a kick ?

You need the following, maybe when you touch the screen / or a button

[_myBall.physicsBody applyImpulse:CGVectorMake(somePowerX, somePowerY)];

Here is another post that will help get you started

Also, this is a good tutorial for beginners.

Upvotes: 8

Related Questions