如何使用销接头在SKSpriteNode周围拖动

时间:2018-10-31 23:30:52

标签: swift sprite-kit

我希望能够拿起SKSpriteNode并实际上用手指“悬挂”它。我一直在尝试SKPhysicsJointPin,但似乎并没有达到预期的效果。

这是我正在使用的代码:

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        let location = touch.location(in: self)

            if !BeingMoved {
                BeingMoved = true
                TempDragNode.name = "TempDragNode"
                TempDragNode.size = CGSize(width: 44, height: 44)
                TempDragNode.physicsBody = SKPhysicsBody(circleOfRadius: 44)

                TempDragNode.physicsBody?.affectedByGravity = false
                addChild(TempDragNode)

                TempDragNode.physicsBody?.allowsRotation = false

                let PositionX = CGPoint(x: TempDragNode.position.x, y: TempDragNode.position.y+(TempDragNode.frame.height/2))

                let PinJoint = SKPhysicsJointPin.joint(withBodyA: (childNode(withName: "Nutty")?.physicsBody)!, bodyB: TempDragNode.physicsBody!, anchor: PositionX)

                scene?.physicsWorld.add(PinJoint)


            }

            TempDragNode.position = location
        }
}

0 个答案:

没有答案