3

I have a SKSpriteNode with a lot of child nodes. I use that node with all the children very frequently in my game.

A better approach would be to build this node once with all the children, create an image (SKTexture) and use the created texture, as opposed to recreating the SKSpriteNode and all its children.

Is there any way to do this?

Farini
  • 923
  • 1
  • 18
  • 35
  • Could you please give more details about how you initialize this SKSpriteNode, what it looks like (do you have a custom class for it?) and where/when do you need to use it (many times in one single Scene, or many times in many different Scenes) ? –  Sep 24 '17 at 13:32
  • Are you sure that having this node and it’s children is causing performance issue? – Steve Ives Sep 24 '17 at 14:26

2 Answers2

0

SKEffectNode & shouldRasterize

You can us SKEffectNode and set shouldRasterize to true.

let effectNode = SKEffectNode()
effectNode.shouldRasterize = true
scene.addChild(effectNode)

The SKEffectNode's output is rasterized and cached internally. This cache is reused when rendering.

Now just add your sprites to effectNode

effectNode.addChild(sprite0)
effectNode.addChild(sprite1)
effectNode.addChild(sprite2)

The first time the effectNode is rendered, the produced image will be automatically cached. And the cached version will be used for the next frames.

Luca Angeletti
  • 58,465
  • 13
  • 121
  • 148
0

There is actually a very easy way to do this.. I do this all the time to convert ShapeNodes to SpriteNodes (because shapes need 1 draw call per shape, and sprites dont)

let texture = SKView().texture(from: desiredNode)
let newSprite = SKSpriteNode(texture: texture)

or, in your gamescene for brevity:
let newSprite = SKSpriteNode(texture: view!.texture(from: desiredNode))

Fluidity
  • 3,985
  • 1
  • 13
  • 34