SKPhysicsJointLimit EXC_BAD_ACCESS?

SKPhysicsJointLimit EXC_BAD_ACCESS?

我正在使用 Sprite Kit (iOS),但每当我尝试将 SKPhysicsJointLimit 添加到 physicsWorld 时,应用程序就会崩溃并显示 EXC_BAD_ACCESS (code=1, address=0xc0)。其他关节类型工作正常,这让我感到困惑。这是崩溃的示例:

var node1 = SKSpriteNode(color: SKColor.blueColor(), size: CGSize(width: 50, height: 50))
node1.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: 50, height: 50))
self.addChild(node1)

var node2 = SKSpriteNode(color: SKColor.blueColor(), size: CGSize(width: 50, height: 50))
node2.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: 50, height: 50))
self.addChild(node2)

var joint = SKPhysicsJointLimit()
joint.maxLength = 1000
joint.bodyA = node1.physicsBody
joint.bodyB = node2.physicsBody
self.physicsWorld.addJoint(joint)

当我将 SKPhysicsJointLimit() 替换为 SKPhysicsJointFixed()(并删除线设置 maxLength)或其他一些联合类型时,代码按预期工作。

我是 Sprite Kit 的新手,有什么解决办法吗?

应用程序崩溃,因为您没有设置关节的锚点属性。从文档中,anchorA

A connection point on the first body in the scene’s coordinate system.

并且anchorB

A connection point on the second body in the scene’s coordinate system.

下面是一个示例,说明如何创建一个 SKPhysicsJointLimit 以物理体和锚点作为参数的对象:

    let joint = SKPhysicsJointLimit.jointWithBodyA(node1.physicsBody!, bodyB: node2.physicsBody!, anchorA: node1.position, anchorB: node2.position)
    joint.maxLength = 1000
    physicsWorld.addJoint(joint)

不知道能不能直接设置锚点