Sam Sam - 6 months ago 24
Swift Question

Getting an exact node count and performance issues in Swift/SpriteKit

I'm making my first game with SpriteKit in which enemies come on screen from one side and pass through- going off screen on the other side. I noticed that later on in the game when different types of enemies are being rendered the FPS drops and the CPU usage approaches 100% (~95-99%). I wanted to know if there was a way to get the exact node count on scene (Not just ones rendered on screen), to tell if I wasn't properly removing them. I already have a global node counter that I update and it seems to work properly-- the total node count is generally consistent. Are there other things that I can do to try and debug this? Thanks!


You can also create an extension of SKNode to calculate all the nodes in the subtree starting from the current node.

extension SKNode {
    func subtreeCount() -> Int {
        return children.reduce(1) { $0 + $1.subtreeCount() }

Now inside your scene simply write

let totalNodes = subtreeCount()