1

UPDATE

More info here: Poor performance with SKShapeNode in Sprite Kit

I'm learning the new Swift language. To challenge myself, I'm trying to build a Curve Fever clone with SpriteKit on iOS 8. I'm using CGPath to draw a path behind the player (a circle).

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)
}

This works perfectly. However... After about 10 seconds the FPS goes rapidly from 60 FPS to 9 FPS. I also noticed that the memory usage is about 100 MB and more. This is not normal for a simple game that draws a moving circle and a path. Is this the correct way to do it?

Community
  • 1
  • 1
Hans Ott
  • 599
  • 7
  • 11

1 Answers1

0

Removing and re-adding the SKShapeNode is superfluous and adversely affects performance:

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

This should suffice:

 line.path = linePath // Update path

Also worth noting is that you ought to release the path when you're done using it via CGPathRelease(linePath).

Also note that if you're testing on the Simulator the performance will quickly drop because it uses a software renderer.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217