固定关节的锚点与节点错位 - 快速

时间:2017-11-02 16:08:22

标签: ios sprite-kit skphysicsbody skshapenode skphysicsjoint

我有一个有两个节点的场景,一个是形状节点,另一个是精灵节点。我添加到场景中并定义其各自的物理主体属性。一切看起来都很好,直到我在两个节点之间添加一个新的固定关节,因为你可以看到物理调试在屏幕上显示一条对角线,我不知道这意味着什么,锚点似乎是错位的。

注意:锚节点是1x1大小的图像

这是代码

class GameScene: SKScene, SKPhysicsContactDelegate {

    private var anchor = SKSpriteNode(imageNamed: "anchor")
    private var curve: SKShapeNode!

    override func didMove(to view: SKView) {
        self.physicsWorld.contactDelegate = self

        self.addAnchor()
        self.addCurve()

        // This is the code is causing the diagonal line
        let jointOneFixed = SKPhysicsJointFixed.joint(withBodyA: anchor.physicsBody!, bodyB: curve.physicsBody!, anchor: anchor.position)
        self.physicsWorld.add(jointOneFixed)
    }

    func addAnchor(){
        anchor.position = CGPoint(x: self.size.width / 2, y: self.size.height + 1)
        anchor.anchorPoint = CGPoint(x: 0.5, y: 0.5)
        anchor.setScale(1)
        anchor.zPosition = 2
        anchor.name = "anchor"
        anchor.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: anchor.size.width, height: anchor.size.height))
        anchor.physicsBody?.isDynamic = false
        anchor.physicsBody?.affectedByGravity = false
        anchor.physicsBody?.allowsRotation = true
        self.addChild(anchor)
    }

    func addCurve() {
        let startPoint = CGPoint(x: anchor.position.x, y: anchor.position.y)
        let endPoint = CGPoint(x: self.size.width / 2, y: self.size.height - 400)

        // Create line with SKShapeNode
        curve = SKShapeNode()
        curve.zPosition = 9
        let path = UIBezierPath()
        path.move(to: startPoint)

        path.addCurve(to: endPoint, controlPoint1: CGPoint(x: endPoint.x - 50, y: self.size.height - 140), controlPoint2: CGPoint(x: endPoint.x + 70, y: self.size.height - 260))

        curve.path = path.cgPath
        curve.strokeColor = UIColor.red
        curve.lineWidth = 9
        curve.name = "curve"
        curve.physicsBody = SKPhysicsBody(edgeLoopFrom: curve.path!)
        self.addChild(curve)
    }
}

这是截图(我还不能在我的帖子中包含图片,所以这是链接) Screenshot

0 个答案:

没有答案