SpriteKit Swift Node Count issues

2020-02-06 09:50发布

The simulator shows the node count, that there are 2 nodes. But why is that? Since there is only one var(ball). Is there a way to create a circle without having it be double the nodes.

class GameScene: SKScene {

var ball = SKShapeNode(circleOfRadius: 50)

override func didMoveToView(view: SKView) {

    ball.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2)
    ball.fillColor = SKColor.blackColor()
    self.addChild(ball)
}

1条回答
ゆ 、 Hurt°
2楼-- · 2020-02-06 10:35

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.

查看更多
登录 后发表回答