【问题标题】:How to solve poor CGPath performance? [Curve Fever clone in iOS 8 / SpriteKit]如何解决较差的 CGPath 性能? [iOS 8 / SpriteKit 中的 Curve Fever 克隆]
【发布时间】:2014-11-20 00:06:49
【问题描述】:

更新

更多信息在这里: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 快速变为 9 FPS。我还注意到内存使用量约为 100 MB 甚至更多。这对于绘制移动圆圈和路径的简单游戏来说是不正常的。这是正确的做法吗?

【问题讨论】:

    标签: ios swift ios8 sprite-kit


    【解决方案1】:

    删除和重新添加 SKShapeNode 是多余的,会对性能产生不利影响:

     line.removeFromParent()
     line.path = linePath // Update path
     self.addChild(line)
    

    这就足够了:

     line.path = linePath // Update path
    

    另外值得注意的是,当你使用完路径后,你应该通过CGPathRelease(linePath)释放路径。

    另请注意,如果您在模拟器上进行测试,性能会迅速下降,因为它使用software renderer

    【讨论】:

    • 当前 CGPathRelease 由于自动内存管理不可用。
    猜你喜欢
    • 1970-01-01
    • 2017-10-03
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    • 1970-01-01
    • 2015-09-01
    • 1970-01-01
    • 2015-06-18
    相关资源
    最近更新 更多