测试代码如下:
var paths = UIBezierPath()
var bottomLeft = CGPoint(x: dim.minX, y:dim.minY)
var bottomRight = CGPoint(x: dim.maxX , y: dim.minY )
var topRight = CGPoint(x: dim.maxX, y: dim.maxY )
var topLeft = CGPoint(x: dim.minX, y: dim.maxY)
paths.moveToPoint(bottomRight)
paths.addLineToPoint(topRight)
paths.moveToPoint(topRight)
paths.addLineToPoint(topLeft)
paths.moveToPoint(topLeft)
paths.addLineToPoint(bottomLeft)
paths.moveToPoint(bottomLeft)
paths.addLineToPoint(bottomRight)
var n = SKShapeNode(path: paths.CGPath)
n.strokeColor = UIColor.greenColor()
n.physicsBody = SKPhysicsBody(edgeLoopFromPath: paths.CGPath)
shapeLayer.addChild(n)
它绘制了一个标准的矩形。我将在从右上角到左上角点的中间添加一些曲线,但我想用简单的矩形来测试它。我正在测试与球的碰撞,它似乎只与从 bottomRight 到 topRight 的线发生碰撞,而忽略了其余部分。有谁知道这里发生了什么?
最佳答案
虽然我不确定贝塞尔路径的问题和所需的物理碰撞设置代码,但是我能够使用原始路径和添加行让它工作:
var polygonPath = CGPathCreateMutable()
CGPathMoveToPoint(polygonPath, nil, bottomRight.x, bottomRight.y)
CGPathAddLineToPoint(polygonPath, nil, topRight.x, topRight.y)
CGPathAddLineToPoint(polygonPath, nil, topLeft.x, topLeft.y)
CGPathAddLineToPoint(polygonPath, nil, bottomLeft.x, bottomLeft.y)
CGPathCloseSubpath(polygonPath)
var n = SKShapeNode(path: polygonPath)
n.strokeColor = UIColor.grayColor()
n.physicsBody = SKPhysicsBody(edgeLoopFromPath: polygonPath)
这会设置适当的物理碰撞边界。虽然这解决了我的问题,但任何关于为什么原始代码不能使用贝塞尔曲线路径的见解都会很棒。谢谢。
关于swift - SpriteKit PhysicsBody 没有应用我的贝塞尔曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26112588/