desperado desperado - 1 year ago 57
Swift Question

Creating SKSpriteNode from SKTexture using pixeldata

I'm trying to create a SKSpriteNode from a 2D-Array of RGBA objects which represents the pixels in a image I want to create. But I don't know how to do it, and I searched for tutorials and could not find one.

For example:

struct RGBA {
var r: UInt8
var g: UInt8
var b: UInt8
var a: UInt8

let pixelData: [[RGBA]] = ...

function CreateSKSpriteNodeFromPixelData(data: [[RGBA]]) -> SKSpriteNode {
//......What goes on here?

Thank you very much for your help!

Answer Source

You need to create texture from RGBA pixel data and use it as constructor parameter for SKSpriteNode.

let texture = SKTexture(data: pixelData, size: pixelDataSize)
let sprite = SKSpriteNode(texture: texture)

SKTexture reference