无法将关节连接的精灵移动到正确的位置

时间:2013-11-06 18:07:54

标签: ios physics sprite-kit

我有一个由SKPhysicsJoints连接的牵线木偶。我希望它表现得如此,当我触摸场景中的某个点时,牵线木偶的头移动到那一点。问题是,当我这样做的时候,头部朝向那个点移动,但是没有到达它。如果我一直点击相同的点,每次都会更接近。

我在构成牵线木偶的所有SKSpriteNodes上设置了affectedByGravity = NO。

我正在通过

移动精灵
head.position = (the position of the mouse)

只要头部没有通过关节连接到木偶的其余部分,它就可以正常工作。

所以有点像其他的精灵正在压低头部。我希望我能更多地了解这一点,关于Sprite Kit的信息很少,而且从我能找到的内容来看,这并不是很多。

编辑:自从我被问及以来,这是一个展示我遇到的问题的示例项目:

-(void) didMoveToView:(SKView *)view
{
self.physicsWorld.speed = 1;
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];

_head = [SKSpriteNode spriteNodeWithImageNamed:@"head.png"];
_chest = [SKSpriteNode spriteNodeWithImageNamed:@"chest_neck.png"];

_head.position = CGPointMake(512, 380);
_chest.position = CGPointMake(512, 290);

_head.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:_head.size];
_head.physicsBody.mass = 1;
_head.physicsBody.dynamic = YES;

_chest.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:_chest.size];
_chest.physicsBody.mass = 1;
_chest.physicsBody.dynamic = YES;

[self addChild:_head];
[self addChild:_chest];

CGPoint chestJointPinAnchor = CGPointMake(_chest.position.x, _chest.position.y+39);
SKPhysicsJointPin *chestJointPin = [SKPhysicsJointPin jointWithBodyA:_head.physicsBody bodyB:_chest.physicsBody anchor:chestJointPinAnchor];
[self.physicsWorld addJoint:chestJointPin];
}

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

_head.physicsBody.affectedByGravity = NO;
_chest.physicsBody.affectedByGravity = NO;

UITouch *touch = [touches anyObject];
CGPoint positionInScene = [touch locationInNode:self];
_head.position = positionInScene;
}

1 个答案:

答案 0 :(得分:1)

只需将头放在:_head.physicsBody.dynamic = NO,胸部保持活力:_chest.physicsBody.dynamic = YES

你会发现头部现在正好移动到触摸位置,拖着胸部。