我的 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 - 这也与此有关保持SKSpriteNode与CGPath中SKShapeNode的相对位置 但也没有任何反应:(
编辑,当我运行此命令时:
let testSprite = SKSpriteNode(color: UIColor.yellow, size: trackViewSprite.size)
self.addChild(testSprite)
经过长时间的讨论,我们确定问题是由于框架尺寸不是形状的预期尺寸造成的。
为了解决这个问题,OP 创建了原始路径的外部路径,并计算了围绕该路径的框架。现在这种方法可能并不适合所有人。
如果其他人遇到此问题,他们将需要执行以下操作:
1)检查SKShapeNode的框架以确保其正确
2) 确定哪种方法最适合计算正确的所需帧
3) 当获得时使用这个新框架仅提取所需的纹理大小textureFromNode
我刚刚遇到了这个问题,并以类似于接受的答案的方式解决了它 - 但我注意到,node.calculateAccumulatedFrame() 似乎并不总是正确计算高斯模糊,并且经常会剪辑模糊 - 这看起来很糟糕。我的解决方案-
须藤代码:
let texBounds = shapeNode.calculateAccumulatedFrame()
var largestDim = max(texBounds.width, texBounds.height) * 1.1
let viewDim = max(view!.bounds.width, view!.bounds.height)
largestDim = min(largestDim, viewDim)
let cropBounds = CGRect(x: -1.0 * largestDim, y: -1.0 * largestDim, width: largestDim * 2, height: largestDim * 2)
let tex = view?.texture(from: alphaNode, crop: cropBounds)
texNode = SKSpriteNode(texture: tex)