【问题标题】:Can't change SKPhysicsJointLimit maxLength after adding the joint to SKPhysicsWorld in Sprite Kit将关节​​添加到 Sprite Kit 中的 SKPhysicsWorld 后无法更改 SKPhysicsJointLimit maxLength
【发布时间】:2014-03-17 22:23:29
【问题描述】:

所以,我创建了一个 SKSpriteNode 环,它们本质上是使用销接头连接在一起的矩形。我想将此环悬挂在 SKShapeNode 圆圈内。我已使用 SKPhysicsJointLimit 将每个节点连接到 SKShapeNode。

这一切都很好,如果我将 maxLength 设置为我主观确定的“正确”数字,我就会得到我正在寻找的效果。

我已将所有限制关节存储在一个数组中,因此我可以轻松访问,并将它们添加到物理世界中。

所有这些都按预期正常工作,尽管需要一些工作才能使代码正确。这些东西很挑剔!

现在,我真正想做的是将 maxLength 设置为某个较大的数字,然后随着时间的推移逐渐减少该数字,使戒指从“松散悬挂”变为“拉紧”

我尝试了多种方法,但似乎都没有奏效。首先,我试过了:

SKAction *tighten = [SKAction customActionWithDuration:1 actionBlock:^(SKNode *node, CGFloat elapsedTime) {
    CGFloat t = elapsedTime/duration;
    CGFloat p = t*t;
    CGFloat newLimit = fromLimit*(1-p) + toLimit*p;
    for (int i = 0; i < _connectors.count; i++){
        [[_connectors objectAtIndex:i] setMaxLength:newLimit];
    }
}];

[_ring runAction:tighten];

但这根本没有做任何事情。

我终于意识到,在将关节添加到物理世界后,您似乎无法更改关节的属性。所以,我在场景的更新方法中尝试了这样的事情:

        [scene.physicsWorld removeJoint:[_connectors objectAtIndex:i]];
        [[_connectors objectAtIndex:i] setMaxLength:newLimit];
        [scene.physicsWorld addJoint:[_connectors objectAtIndex:i]];

但是,这也不起作用。它抛出了错误:

'Cant add joint <PKPhysicsJointRope: 0x114e84350>, already exists in a world'

即使我已经明确删除了它。所以,我真的不知道从这里去哪里。互联网上关于关节的文档和现有材料非常薄。我想接下来我会尝试移除所有关节并添加它们,但这将是一个巨大的痛苦。

任何想法都会非常有帮助!

【问题讨论】:

  • 我解决了这个问题。没关系。我试图在返回对象之前进行更改。笨蛋。
  • 我遇到了和你一样的问题。您能否提供更多详细信息?

标签: sprite-kit skphysicsbody skphysicsworld


【解决方案1】:

正如 OP 所指出的,在应用更改之前,关节似乎必须从物理世界中移除。以下对我有用。

for joint in joints
{
    physicsWorld.removeJoint(joint)
    joint.maxLength = yourNewValue
    physicsWorld.addJoint(joint)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-09
    • 1970-01-01
    相关资源
    最近更新 更多