拖动通过SKPhysicsJoints附加的Sprite的最佳方法?

时间:2013-10-30 20:48:21

标签: ios physics sprite-kit

我无法移动通过SKPhysicsJoint附加的精灵。我有一个touchesMoved方法,如下所示:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];
    CGPoint previousPosition = [touch previousLocationInNode:self];
    CGPoint translation = CGPointMake(positionInScene.x - previousPosition.x, positionInScene.y - previousPosition.y);
    CGPoint newPosition = CGPointMake(_selectedNode.position.x + translation.x, _selectedNode.position.y + translation.y);
    [_selectedNode runAction:[SKAction moveTo:newPosition duration:0]];
}

并且它有点工作,但是如果我将重力设置得非常低,它似乎只能将精灵移动25%,然后如果我将重力设置为重力,那么它几乎不会在y轴上移动它默认。我很困惑,它可能是很多东西,但我想也许我只需要设置速度或其他东西,但如果是这样,那么什么是合适的设置?

1 个答案:

答案 0 :(得分:1)

这应该可以解决问题,我还建议你在抓住精灵时关闭物理,然后在释放时再次使用。

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

    UITouch *touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];
    CGPoint previousPosition = [touch previousLocationInNode:self];
    CGPoint translation = CGPointMake(positionInScene.x - previousPosition.x, positionInScene.y - previousPosition.y);
    //_selectedNode.physicsBody.dynamic = NO;
    [_selectedNode setPosition:CGPointMake(_selectedNode.position.x + translation.x, _selectedNode.position.y + translation.y)];
}

如果你想再次开启物理学

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    _selectedNode.physicsBody.dynamic = YES;
}