Search code examples
iosswiftsprite-kitskphysicsbodyskshapenode

Use PhysicsBody with SKShapenode line. Swift, iOS, SpriteKit


I need to add simple line using ShapeBody to interact with. I'am trying code below, by Xcode gives me next response:

PhysicsBody: Could not create physics body.

        let testPath = UIBezierPath()
        testPath.move(to: CGPoint(x:-100, y: 200))
        testPath.addLine(to: CGPoint(x:100, y: 200))

        let testShape = SKShapeNode()
        testShape.path = testPath.cgPath
        testShape.position = CGPoint(x:0, y:250)
        testShape.zPosition = 5
        testShape.lineWidth = 5
        testShape.strokeColor = .red
        testShape.physicsBody = SKPhysicsBody(polygonFrom: testPath.cgPath)

Solution

  • Used edgeChainFrom instead of polygonFrom, and it works!

    testShape.physicsBody = SKPhysicsBody(edgeChainFrom: testPath.cgPath)