更新
更多信息在这里:Poor performance with SKShapeNode in Sprite Kit
我正在学习新的 Swift 语言。为了挑战自己,我尝试在 iOS 8 上使用 SpriteKit 构建一个 Curve Fever 克隆。我正在使用 CGPath 在玩家身后绘制一条路径(一个圆圈)。
var linePath : CGMutablePathRef = CGPathCreateMutable()
var line : SKShapedNode!
override func update(currentTime: CFTimeInterval) {
// Adjust player's location on the field based on touches left or right
...
// Get the previous and current point
var previousPoint : CGPoint = ...
var currentPoint : CGPoint = ...
// Add new line (from previous to current point) to the existing path
CGPathMoveToPoint(linePath, nil, previousPoint.x, previousPoint.y)
CGPathAddLineToPoint(linePath, nil, currentPoint.x, currentPoint.y)
// Redraw line
line.removeFromParent()
line.path = linePath // Update path
self.addChild(line)
}
这非常有效。但是...大约 10 秒后,FPS 从 60 FPS swift 上升到 9 FPS。我还注意到内存使用量约为 100 MB 甚至更多。这对于绘制移动圆圈和路径的简单游戏来说是不正常的。这是正确的做法吗?
最佳答案
删除并重新添加 SKShapeNode 是多余的并且会对性能产生不利影响:
line.removeFromParent()
line.path = linePath // Update path
self.addChild(line)
这应该足够了:
line.path = linePath // Update path
另外值得注意的是,当您使用完路径后,您应该通过 CGPathRelease(linePath)
释放路径。
另请注意,如果您在模拟器上进行测试,性能会 swift 下降,因为它使用了 software renderer .
关于ios - 如何解决 CGPath 性能不佳的问题? [iOS 8/SpriteKit 中的 Curve Fever 克隆],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27029468/