我需要使用ShapeBody添加简单的线来进行交互。 我正在尝试下面的代码,Xcode给了我下一个回复:
PhysicsBody:无法创建物理体。
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)
答案 0 :(得分:0)
使用edgeChainFrom而不是polygonFrom,它可以工作!
testShape.physicsBody = SKPhysicsBody(edgeChainFrom: testPath.cgPath)
答案 1 :(得分:0)
路径不能与SKPhysicsBody的任何线相交,你的线需要是一个细长的矩形
let testPath = UIBezierPath()
testPath.move(to: CGPoint(x:-100, y: 200))
testPath.addLine(to: CGPoint(x:100, y: 200))
testPath.addLine(to: CGPoint(x:100, y: 201))
testPath.addLine(to: CGPoint(x:-100, y: 201))
testPath.close()
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)