ios - 如何解决 CGPath 性能不佳的问题? [iOS 8/SpriteKit 中的 Curve Fever 克隆]

标签 ios swift ios8 sprite-kit

更新

更多信息在这里: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/

相关文章:

ios - 并行多个快速动画不起作用

ios - 在 iOS 上以测试用户身份登录 Facebook 开发应用程序

ios - 如何将 IBOutlet 值从 View 传递给 VIPER iO 中的交互器?

ios - swift 4 : unrecognized selector sent to instance ERROR

ios - 如何支持 iOs 9 功能,同时保持对 iOs 8 的支持?

ios - 在 iOS 8 中编辑照片 : Allow "YourApp" to modify this photo?

objective-c - 如果需要,正确使用格式说明符最多显示三位小数,否则显示零位小数?

ios - 在实例化时初始化一个新数组

swift - 尽管有多种方法,标签仍未在容器内调整大小

swift - 如何使用 Swift 在 Xcode 6 中执行自动转场?