Swift SpriteKit SKPhysicsJointPin

时间:2015-03-28 18:06:01

标签: swift sprite-kit skphysicsjoint

我正在尝试在Swift SpriteKit中实现一条绳子并为其添加物理效果,这个位置对所有人都有好处,但它们不会附着,当我击中游戏时它们都会掉落,除了第一个是“持有人”。这是我的代码:

    // create rope holder
    let chainHolder = SKSpriteNode(imageNamed: "chainHolder")

    chainHolder.position.y = self.frame.maxY - chainHolder.size.height

    chainHolder.physicsBody = SKPhysicsBody(circleOfRadius: chainHolder.size.width / 2)
    chainHolder.physicsBody?.dynamic = false
    //chainHolder.physicsBody?.allowsRotation = true

    chains.append(chainHolder)
    addChild(chainHolder)

    // add each of the rope parts
    for i in 0...5 {

        let chainRing = SKSpriteNode(imageNamed: "chainRing")
        let offset = chainRing.size.height * CGFloat(i + 1)
        chainRing.position = CGPointMake(chainHolder.position.x, chainHolder.position.y - offset)
        chainRing.name = String(i)

        chainRing.physicsBody = SKPhysicsBody(rectangleOfSize: chainRing.size)
        //chainRing.physicsBody?.allowsRotation = true

        chains.append(chainRing)
        addChild(chainRing)
    }

    // set up joints between rope parts
    for i in 1...5 {

        var nodeA = chains[i - 1]
        var nodeB = chains[i]
        var joint = SKPhysicsJointPin.jointWithBodyA(nodeA.physicsBody, bodyB: nodeB.physicsBody,
            anchor: CGPointMake(CGRectGetMidX(nodeA.frame), CGRectGetMinY(nodeA.frame)))

        physicsWorld.addJoint(joint)
    }

1 个答案:

答案 0 :(得分:0)

我发现问题是因为我在(0.5,0.5)中为场景设置了锚点。如果我把它留在(0,0),一切都还可以。