FPS crashes and game slows down - Sprite-Kit and Swift

I have a game using Sprite-Kit and Swift, where I generate random circles falling from the top of the screen to the bottom of the screen.

When the game starts, it works fine at the beginning (about 60 FPS or less), but then the FPS gradually drops, and the game becomes very slow ... I don’t understand why the FPS falls over time (the number of nodes remains good around 8-10, so they are deleted when they leave the screen). I tested it both on the iOS simulator and on the device itself, any ideas?

I checked the problem is not a memory leak. In addition, I use only one view controller.

The only function that I think could cause this problem is this, but I don't know why:

/* Function to generate single random circle */
func generateCircle() -> Void {
    let circleSize:CGFloat = CGFloat(arc4random_uniform(40) + 3)
    let xPosition:CGFloat = CGFloat(arc4random_uniform(UInt32(size.width)))

    var randomCircle = SKShapeNode(circleOfRadius: circleSize)
    randomCircle.strokeColor = SKColor.redColor()
    randomCircle.fillColor = SKColor.redColor()
    randomCircle.physicsBody = SKPhysicsBody(circleOfRadius: circleSize)
    randomCircle.physicsBody?.dynamic = false
    randomCircle.position = CGPoint(x: xPosition, y: size.height + circleSize*2)
    randomCircle.physicsBody?.dynamic = true
    randomCircle.physicsBody?.categoryBitMask = randomCirclesGroup
    addChild(randomCircle)
}
+4
3

, .

  • Xcode.
  • Xcode Debug Navigator CMD + 6.
  • , , .

, , , , .

, , - .

+2

, node , , .

, .

( , - )

 let circles = Set<SKShapeNode>()

generateCircle() :

 circles.insert(randomCircle)

update():

 override func update(currentTime: CFTimeInterval){
      for index in circles {
           if index.position.y <= 0 {
                index.removeFromParent()
                circles.remove(index)
           }
      }
 }

, , , - , , .

+1

SKShapeNode SKSpriteNode, SKShapeNode, .

Apple:

, . . SKSpriteNode , .

SKShapeNode (, , ), :

  • , , .
  • . , . [CGSize: SKTexture].
  • , , SKSpriteNode.

You can use the SKView function to render SKShapeNodeto texture :textureFromNode

Displays the contents of the node tree and returns the displayed image as SpriteKit textures.

0
source

Source: https://habr.com/ru/post/1570197/


All Articles