1

我有一个通过 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;
}
4

1 回答 1

1

只需将头设置为:_head.physicsBody.dynamic = NO,而胸部保持动态:_chest.physicsBody.dynamic = YES

您会发现头部现在精确地移动到触摸位置,同时拖动胸部。

于 2014-01-15T14:12:43.983 回答