Spritekit Swift Node Count Issues

SpriteKit Swift Node Count issues

This is happening because SKShapeNode fillColor is set. Obviously, when either fillColor or strokeColor are set, each of them require additional node and additional draw pass . By default there is already strokeColor set (one node, one draw pass) and because of fillColor is set additionally, two nodes (and two drawing passes) are required for shape to be rendered.

SKShapeNode is not a performant solution in many cases, because of obvious reasons. One SKShapeNode requires at least one draw call. There are some interesting post related to this topic, for example this one or this one.

But still IMO, SKShapeNode should be avoided if it's not the only solution. Just because of performance reasons. And in your case, if you for example want to have many different kind of balls, the performant solution would be to use texture atlas and SKSpriteNode for representing those textures. This way you can render as much balls as you like in single draw pass.

Note that draw passes are much more important than number of nodes when it comes to performance. SpriteKit can render hundreds of nodes at 60fps if amount of drawing passes required for scene rendering is low.

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

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

How do I get the node count of SKSpriteNodes not visible in the scene?

Because showsNodeCount only shows the on-screen nodes, you'd have to create your own SKSpriteNode class and create a method like addMyChild. And whenever you call the addMyChild method you increase a counter by one so that you manually know, how many nodes exist. for example:

func addMyChild(node:SKSpriteNode){
self.addChild(node)
nodeCount++
}

How to keep node count of invaders in space game stable in xcode spritekit with swift?

Particularly i think that it's not a good idea to put this code on the update func as it will run your code before render every frame...so it will run it like 60 times per second.

A better solution is create a Function to spawn enemies and call it with a timer. First of all declare a variable in your class to handle the enemy count and the timer:

var enemies = 0
var enemyTimer = Timer()

func spawn(){
//YOUR SPAWN CODE
enemies += 1 //Will increase 1 enemy to the enemies variable every time this function is called
}

to start the timer:

 enemyTimer = Timer.scheduledTimer(timeInterval: YOUR TIME INTERVAL TO SPAWN ENEMIES, target: self, selector: #selector(YOUR_CLASS_NAME.spawn), userInfo: nil, repeats: true)

Start the timer when the game starts, invalidate it when the game ends and when you move to another scene!
This timer will call the spawn func every "n" seconds!

About the out of screen you may use the update func:

      if scene?.frame.contains(enemyNode.position){
//nothing here
}else{
//Out of screen, do stuff here...
enemyNode.removeFromParent()
}

Why does my simulator still show node count and FPS? [SpriteKit]

The default SpriteKit template sets node count and fps after the initialization of the scene in ViewController.m, you have to remove these lines.

High SpriteKit draw count with SKSpriteNode & texture atlas

I solved the problem by setting zPosition to the same value on both the parent and child.

parentNode.zPosition = 1.0
childNode.zPosition = 1.0

There are now a total of 2 draws for the balls regardless of how many balls there are.

Sprite appearing in node count but not visible

I assume that the background node size is just the same as the image size. I also assume that the scene size is the same as the image size. If that's the case, try the following.

let backTexture = SKTexture(imageNamed: "background")
let background = SKSpriteNode(texture: backTexture)
//background.size = CGSize.init(width: (self.scene?.size.width)!, height: (self.scene?.size.height)!)
//background.anchorPoint = self.anchorPoint
background.position = CGPoint(x: CGFloat(i) * background.size.width, y: 0))

Reducing number of draw counts (passes) in SpriteKit

Draw counts are the result of needing to layer a scene in the right order, and some nodes can be dependent on the content behind it if they have some transparency for example.

1: One of the biggest ways to reduce the draw count is to render multiple nodes at once - this can be achieved by adding child nodes you want in the same layer to a parent node. For example, have a parent node called nodeCollection and use addChild() to add multiple nodes to be rendered together to lower the performance hit.

2: Another thing you can do is set ignoresSiblingOrder to true on SKView, and use zPosition on each node instead - this means that SpriteKit has to do less work as you are more explicit.

3: Don't use SpriteKit for UI! This is what UIKit is for! You have your SKView which holds your SKScene. SKView is a subclass of UIView, which means that you have it placed in one of your view controllers. All you need to do is add move views or buttons or whatever you want in your view controller on top of your SKView, and you're set!

For more optimisations I recommend checking out: Hacking with Swift - 15 tips to optimize your SpriteKit game.



Related Topics



Leave a reply



Submit