我在我的GameScene中有这个,在didMove()
中调用for i in 1...5 {
// path to create the circle
let path = UIBezierPath(arcCenter: CGPoint(x: center.x, y: center.y), radius: CGFloat(((43 * i) + 140)), startAngle: CGFloat(GLKMathDegreesToRadians(-50)), endAngle: CGFloat(M_PI * 2), clockwise: false)
// the inside edge of the circle used for creating its physics body
let innerPath = UIBezierPath(arcCenter: CGPoint(x: center.x, y: center.y), radius: CGFloat(((43 * i) + 130)), startAngle: CGFloat(GLKMathDegreesToRadians(-50)), endAngle: CGFloat(M_PI * 2), clockwise: false)
// create a shape from the path and customize it
let shape = SKShapeNode(path: path.cgPath)
shape.lineWidth = 20
shape.strokeColor = UIColor(red:0.98, green:0.99, blue:0.99, alpha:1.00)
// create a texture and apply it to the sprite
let trackViewTexture = self.view!.texture(from: shape)
let trackViewSprite = SKSpriteNode(texture: trackViewTexture)
trackViewSprite.physicsBody = SKPhysicsBody(edgeChainFrom: innerPath.cgPath)
self.addChild(trackViewSprite)
}
它使用UIBezierPaths制作几个圆圈。它将路径转换为SKShapeNode,然后转换为SKTexture,然后将其应用于最终的SKSpriteNode。
当我这样做时,SKSpriteNode不在它应该的位置,它是右边的几个:
但是当我添加我创建的SKShapeNode时,它设置得非常好,应该是:
即使这样做也不是它的中心!
trackViewSprite.position = CGPoint(x: 0, y: 0)
无论我尝试什么,它都不会居中。
为什么会这样?转换为纹理时会出现某种错误吗?
P.S - 这也与此有关Keep relative positions of SKSpriteNode from SKShapeNode from CGPath 但也没有回应:(
编辑,当我运行时:
let testSprite = SKSpriteNode(color: UIColor.yellow, size: trackViewSprite.size)
self.addChild(testSprite)
答案 0 :(得分:2)
经过长时间的讨论,我们确定问题是由于框架尺寸不是形状的预期尺寸。
为了解决这个问题,OP创建了原始路径的外部路径,并计算了围绕此路径的帧。现在这种方法对每个人都不适用。
如果有其他人遇到这个问题,他们将需要做这些事情:
1)检查SKShapeNode的框架以确保它是正确的 2)确定哪种方法最适合计算正确的期望框架
3)在获取textureFromNode
以仅提取所需纹理大小时使用此新帧