应用角度冲动

时间:2014-04-06 06:43:34

标签: sprite-kit physics

嗨,大家好,我的代码是玩家和互相交流的球。我想做的是对球施加力,就像我的球员正在射击它一样。我希望球能够用力从球员身上移开。我怎样才能对此施加Impulse或force。我尝试了很多次,但我是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];
}

1 个答案:

答案 0 :(得分:8)

我想你想要像冲刺一样施加冲动?

当您触摸屏幕/或按钮

时,您需要以下内容
[_myBall.physicsBody applyImpulse:CGVectorMake(somePowerX, somePowerY)];

这是另一个有助于您入门的post

此外,对于初学者来说,这是一个很好的tutorial