iosswiftsprite-kitskspritenodesktexture

How to merge SKSpriteNode into a SKTexture to form new SKSpriteNode in Swift 3?


I'm developing an ios app. I have a SKSpriteNode on screen and I'm getting a drawing from the user that I'm storing as SKTexture. I would like to combine these two and introduce them as a new SKSpriteNode.

THE PROBLEM: I cannot find documentation about how to merge SKSpriteNode into SKTexture. I'm sure there's a way to do this. Would someone have example code for this?


Solution

  • You will probably have to use CoreGraphics to do this. Convert both SKTextures to CGImage using SKTexture.cgImage() method, combine them using CoreGraphics and then create a new texture from the result. SKTexture.init(cgImage:) method let you use a CGImage object to construct the texture.

    I haven't used Core Graphics much, but here are a few links to related reads. I think you will want to use the CGLayer class to tell CoreGraphics how to stack the textures and reuse the sprite texture over time so CoreGraphics can cache it to make things faster.

    Core Graphics uses Quartz 2D to do fast 2D graphics operations. I haven't played much with it, but I think it should be what you are looking for.

    References :

    Keep me posted, I think it's very interesting!

    EDIT 1: You will also have to use an SKMutableTexture on the sprite. That class allows for the texture data to be changed dynamically.