As of june 2020, swift 5 iOS 13.5, I have updated the method “updateLevel” based on @ivanchenhz solution. When overlays are added in the scene, check every “Overlay” nodes out of camera and remove them. I am quite new to swift so please let me know if this is the wrong way to “fix” the issue.
First, let’s see the issue :
- set to true view.showsFPS and view.showsNodeCount in GameViewController.swift
- set an insane amount of gain in setPlayerVelocity(_: ) to very quickly add new layers
func setPlayerVelocity(_ amount:CGFloat)
{
let gain: CGFloat = 100
player.physicsBody!.velocity.dy = max(player.physicsBody!.velocity.dy, amount * gain)
}
Quickly, the amount of nodes is above 10K, 15K, 20K, too much K and FPS goes from 60fps to almost 0
Now, let’s remove nodes not needed anymore since they will never be visible again
func updateLevel()
{
let cameraPos = camera!.position
if cameraPos.y > levelPositionY - size.height
{
createBackgroundOverlay()
while lastOverlayPosition.y < levelPositionY
{
addRandomForegroundOverlay()
}
// Start fix here, remove off screen nodes
enumerateChildNodes(withName: "//Overlay")
{
[weak self] node, _ in
guard let self = self else { return }
let cameraPos = self.camera!.position
if node.position.y + self.backgroundOverlayHeight < cameraPos.y
{
node.removeFromParent()
}
}
// End fix
}
}
Now, the nodes count stay in an acceptable range 150…250 and FPS stay at 60fps
I hope it can be of any help.