Impulse不适用于Sprite Kit

时间:2014-08-31 00:13:35

标签: sprite-kit sprite physics

我试图在每次触摸时向SK中的精灵节点施加一个脉冲,但什么也没发生。触摸正在注册,但没有冲动。

-(id)initWithSize:(CGSize)size {    
 if (self = [super initWithSize:size]) {
     /* Setup your scene here */

    self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
    self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];

    SKSpriteNode *torso = [SKSpriteNode spriteNodeWithColor:[SKColor blueColor] size:CGSizeMake(40, 60)];
    torso.position = CGPointMake (250,250);
    torso.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(40, 60)];
    torso.physicsBody.dynamic = YES;

    [self addChild: torso];

    SKSpriteNode *arm = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(15, 50)];
    arm.position = CGPointMake (235,235);
    arm.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(40, 60)];
    arm.physicsBody.dynamic = YES;

    [self addChild: arm];

    SKPhysicsJointPin *leftArm = [SKPhysicsJointPin jointWithBodyA:torso.physicsBody bodyB:arm.physicsBody anchor:torso.position];
    [self.physicsWorld addJoint:leftArm];


   }
   return self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    [_torso.physicsBody applyImpulse:CGVectorMake(12, 10)];
    NSLog(@"touch");
}

我的精灵节点确实有一个链接到它的另一个节点,但是我在没有附加节点的情况下尝试了它的冲动但它仍然无效。

1 个答案:

答案 0 :(得分:0)

出于某种原因,为了移动我的精灵节点,我必须像这样创建它

_torso = [SKSpriteNode spriteNodeWithColor:[SKColor blueColor] size:CGSizeMake(40, 60)];

而不是

 SKSpriteNode *torso = [SKSpriteNode spriteNodeWithColor:[SKColor blueColor] size:CGSizeMake(40, 60)];

一旦我设置断点并看到_torso为零,我就提到了之前的一些工作并看到了我的错误。